Cocos2d-X development of Chinese chess "six" Game start function implementation, cocos2d-x "six"
In the previous blog, I used Cocos2d-X to develop the game scene of Chinese chess 4. I introduced a start button in the game scene and didn't implement the start function.
Effects of the game start function:
Implementation ideas:
1. Display chess pieces at random positions
2. Move the chess piece to the specified position on the board
Specific implementation methods:
First, define an initStone () function in the SceneGame class to initialize the pawns.
The initStone () function implements three functions.
1. Create a pawn
2. Set the pawn to a random position.
3. Hide pawns
Code in initStone:
// Initialize 32 pawns void SceneGame: initStone () {CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the pawns for (int I = 0; I <32; I ++) {// create pawns _ s [I] = Stone: create (I, _ redSide); addChild (_ s [I]); // set the initial position of a piece to random position _ s [I]-> setPosition (ccp (CCRANDOM_0_1 () * winSize. width, CCRANDOM_0_1 () * winSize. height); // hide the pawn _ s [I]-> setVisible (false );}}
Then, call the initStone () function in the Init function of SceneGame.
Define a Start button's response function Start ()
Start () implements two functions:
1. Display hidden pawns
2. Move the chess piece to the specified position on the board
Start () implementation code
// Start void SceneGame: Start (CCObject *) {// place the pawn for (int I = 0; I <32; I ++) {// display the pawns _ s [I]-> setVisible (true); // move the pawns to the specified position on the board CCMoveTo * move = CCMoveTo: create (1, this-> getStonePos (_ s [I]-> getX (), _ s [I]-> getY ())); _ s [I]-> runAction (move );}}
Zookeeper