Now we can only let our characters walk, the opponent does not act, how to realize the rotation walk?
There are 2 methods, one is the use of the brush frame controller in update, and another in the form of a message mechanism, we try to avoid using the brush frame controller. Let's use the second message mechanism.
We define variables Players_vector [player*] in Gamebasescene to hold all the characters;
When adding a new object, set the property _ismyturn to True for the object
After all the objects have been added Players_vector [player*] All of the Ismyturn are true, indicating that the go method can be called to walk.
Ideas are as follows:
After the Go button click, our characters go first, then we should not respond to click events, we can take the go button to disappear after the walk, set Ismyturn = False, indicating that we give up the right to walk.
Then call the method defined in Richergamecontroller Pickoneplayertogo (), Traverse Players_vector, take out a ismyturn = = True Run Go method
After running, the Ismyturn is set to false;
Then continue to call Pickoneplayertogo (), after the run is finished ismyturn=false;
If a total of 3 characters, run 3 times, all objects ismyturn==false;
At this point we have to set the Ismyturn to true again and let the Go button show up for the next rotation.
The general flow is as follows:
1. When the Go button is clicked, send a message that the button disappears, and when the other characters are done walking, send a message to the Go button to display
2, in the Gamebasescene custom message receive, according to receive the message, to let go button disappears or displays
Boolgamebasescene::init () {....//Add a Message receiver Addnotificationobserver (); returntrue;} The method defines a message that receives a MSG_GO message that is Receivedmsgforgo () Voidgamebasescene::addnotificationobserver () {Notificationcenter :: getinstance ()->addobserver (This,callfunco_selector (Gamebasescene::receivedmsgforgo), MSG_GO,NULL);}
Voidgamebasescene::receivedmsgforgo (object* data) {//The Go menu is placed in the menu container and can then be removed from all submenus for individual processing to facilitate later expansion Intretmsgtype = ( string*) data)->intvalue (); Vector<node*>vecmenuitem = GetMenu ()->getchildren (); Sizewinsize = Director::getinstance ()->getwinsize (); When the received message is 1 o'clock, indicates that you want to display individual menu items, so that the menu moves from outside the screen to 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) *2,0));//rotateby* Rotateby =RotateBy:: Create (1,360,10);//action* Action =spawn::create (moveby,rotateby,null); node->runaction (Moveby);}} else//when the message is 0 o'clock, indicates that you want to hide each menu item, move the menu from the screen to the right side of the 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) *2,0);//rotateby* rotateby =RotateBy:: Create (1,360,10);//action* Action =spawn::create (moveby,rotateby,null); node->runaction (Moveby);}} Log ("RecEived Go message is:%d ", retmsgtype);}
Voidgamebasescene::gobuttoncallback (Cocos2d::ccobject *psender) {... .../...../...///After a go-click on a./////After a visit to the Send GO button Vanishing message notificationcenter::getinstance ()->postnotification (Msg_go,string::create ("0"));p layer1-> Startgo (rowvector,colvector); log ("Go button clicked Over");}
Voidrichergamecontroller::p Ickoneplayertogo () {for (auto It=gamebasescene::p layers_vector.begin (); it!= Gamebasescene::p layers_vector.end (); it++) {richerplayer* Richerplayer = dynamic_cast<richerplayer*> (*it); if ( Richerplayer->getismyturn ()) {routenavigation::getinstance ()->getpath (richerplayer,3,gamebasescene:: Canpassgrid,gamebasescene::tiledrowscount,gamebasescene::tiledcolscount); Richerplayer->startgo ( Routenavigation::getinstance ()->getpathrow_vector (), Routenavigation::getinstance ()->getPathCols_vector () ); return;}} For (auto It=gamebasescene::p layers_vector.begin (); It!=gamebasescene::p layers_vector.end (); it++) {richerplayer* Richerplayer = dynamic_cast<richerplayer*> (*it); Richerplayer->setismyturn (true);} The code runs here, the delegates are gone, send a message that shows the Go button notificationcenter::getinstance ()->postnotification (Msg_go,string::create ("1" ));}
After doing this, he also did a finishing job, showing the figures ' capital and physical strength, and letting the characters get random steps and walk. Simply don't explain it.
Click Download code http://download.csdn.net/detail/lideguo1979/8301181
To be continued ........ .......
Cocos2d-x 3.2 Monopoly Game project development-part tenth realizes the people take turns walking