Cocos2d-x 3.2 Monopoly game project development-the tenth part to achieve people walking in turn, cocos2d-x Project Development
Now we can only let our roles walk, and our opponents do not act. How can we achieve this in turn?
There are two methods: one is to use the frame swiping controller for update, and the other is to use the message mechanism. We try to avoid using the frame swiping controller. Use the second message mechanism.
We define the variable players_vector [Player *] In GameBaseScene to store all roles;
When adding a new object, set attribute _ isMyTurn to true for this object.
In this way, after all objects are added, players_vector [Player *] All isMyTurn values are true, indicating that the go method can be called to walk.
The idea is as follows:
After clicking the go button, our role should go first. Then, we should not respond to the click event again. We can take the go button to disappear and set isMyTurn = false after the go button is clicked, indicates that we give up the walking permission.
Call the pickOnePlayerToGo () method defined in RicherGameController to traverse players_vector and obtain an isMyTurn = true running go method.
After running, isMyTurn is set to false;
Then, call pickOnePlayerToGo (). After running, isMyTurn = false;
Assume there are three roles in total. After running three times, isMyTurn = false for all objects;
In this case, we need to set isMyTurn to true again and display the go button for the next rotation.
The general process is as follows:
1. When the go button is clicked, a message is sent, indicating that the button will disappear. After all other roles are walking, a message is sent to display the go button.
2. Customize message receiving in GameBaseScene to make the go button disappear or display according to the received message
BoolGameBaseScene: init (){.............. // Add the Message Receiver addicationicationobserver (); returntrue;} // This method defines receiving a message from MSG_GO. The message processing function is receivedMsgForGo () voidGameBaseScene: addNotificationObserver () {icationcenter center:: getInstance ()-> addObserver (this, callfuncO_selector (GameBaseScene: receivedMsgForGo), MSG_GO, NULL );}
VoidGameBaseScene: receivedMsgForGo (Object * data) {// place the go menu in the menu container, in the future, you can extract all the sub-menus for separate processing, which facilitates the later expansion of intretMsgType = (String *) data)-> intValue (); Vector <Node *> vecMenuItem = getMenu () -> getChildren (); SizewinSize = Director: getInstance ()-> getWinSize (); // when the received message is 1, each menu item is displayed, move the menu from outside the screen. if (retMsgType = 1) {for (auto it = vecMenuItem. begin (); it! = VecMenuItem. end (); it ++) {Node * node = dynamic_cast <Node *> (* it); MoveBy * moveBy = MoveBy: create (0.3, ccp (-(node-> getContentSize (). width) * 1,360); // RotateBy * rotateBy = RotateBy: create (, 10); // Action * action = Spawn: create (moveBy, rotateBy, NULL); node-> runAction (moveBy) ;}} else // when the received message is 0, each menu item must be hidden, move the menu from the screen to the outside of the right screen {for (auto it = vecMenuItem. begin (); it! = VecMenuItem. end (); it ++) {Node * node = dynamic_cast <Node *> (* it); MoveBy * moveBy = MoveBy: create (0.3, ccp (node-> getContentSize (). width) * 1,360); // RotateBy * rotateBy = RotateBy: create (, 10); // Action * action = Spawn: create (moveBy, rotateBy, NULL); node-> runAction (moveBy) ;}} log ("received go message is: % d", retMsgType );}
VoidGameBaseScene: goButtonCallback (cocos2d: CCObject * pSender ){..................................... // After you click go, send the go button to disappear. icationcenter center: getInstance ()-> postNotification (MSG_GO, String: create ("0 ")); player1-> startGo (rowVector, colVector); log ("go button clicked over ");}
VoidRicherGameController: pickOnePlayerToGo () {for (auto it = GameBaseScene: players_vector.begin (); it! = GameBaseScene: players_vector.end (); it ++) {RicherPlayer * richerPlayer = dynamic_cast <RicherPlayer *> (* it); if (richerPlayer-> getIsMyTurn () {RouteNavigation :: getInstance ()-> getPath (richerPlayer, 3, GameBaseScene: canPassGrid, GameBaseScene: Signature, GameBaseScene: tiledColsCount); richerPlayer-> startGo (RouteNavigation: getInstance () -> getPathRow_vector (), RouteNavigation: getInstance ()-> getPathCols_vec Tor (); return ;}}for (auto it = GameBaseScene: players_vector.begin (); it! = GameBaseScene: players_vector.end (); it ++) {RicherPlayer * richerPlayer = dynamic_cast <RicherPlayer *> (* it); richerPlayer-> setIsMyTurn (true );} // when the code is run here, it means that all are finished. Send the icationcenter center: getInstance ()-> postNotification (MSG_GO, String :: create ("1 "));}
After doing this, I also completed a scanning job to show the money and physical strength of the person, and asked the person to get random steps and then walk. I will not explain it simply.
Click to download code http://download.csdn.net/detail/lideguo1979/8301181
To be continued .....................