Cocos2d-x 3.2 Monopoly game project development-Part 2 crab Road (code), cocos2d-x Project Development

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 2 crab Road (code), cocos2d-x Project Development
In this section, we add a crab wounding event. The Crab genie is randomly added to the road location. When the role finishes walking, if it encounters a crab at the position, the animation is played first, and then the ambulance takes the role away.
When this role is in turn walking, the toast prompts that the hospital is still in hospital for a few days. When the hospital stay expires, the role can continue walking.

Several new genie classes

Item_crab.cpp crab
Item_emergency.cpp ambulance
Item_fog.cpp smog

Item_stretcher.cpp stretcher


// Add these genie void GameBaseScene: initItemSprite () {item_crab = (Item_crab *) Item: create (ITEM_CRAB); addChild (item_crab) in the scenario ); item_crab-> runAction (RepeatForever: create (item_crab-> getNormal_anmi (); item_crab-> setVisible (false); emerg = Item_emergency: create (); addChild (emerg); emerg-> setVisible (false); fog = Item_fog: create (); addChild (fog); fog-> setVisible (false); strentcher = Item_stretcher :: create (); addChild (strentcher); strentcher-> setVisible (false); strentcher-> setAnchorPoint (ccp (0, 0.9);} defines the timer, call this method every 100 seconds. Update the crab location void GameBaseScene: pair () {schedule (schedule_selector (GameBaseScene: updateBlockWaySprites), 100366f);} void GameBaseScene :: updateBlockWaySprites (float dt) {int _ rand1 = rand () % (wayLayerPass_vector.size (); Vec2 position = wayLayerPass_vector.at (_ rand1); position. x-= 5; position. y + = tiledHeight; item_crab-> setVisible (true); item_crab-> setPosition (position); item_crab-> setAnchorPoint (ccp (0, 0.6 ));}

1. First, add the restTimes attribute in RicherPlayer. cpp to record the number of failovers that the role needs to stay in hospital after the injury.
2. Modify void RicherGameController: endGo () method.
When the role finishes walking, send the MSG_BLOCK_WAY_EVENT message. The message is used to process the walking and check whether there are crabs on the road.

void RicherGameController::endGo(){...........if(stepHasGone >= stepsCount){String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BLOCK_WAY_EVENT_TAG,0.0f,0.0f,_richerPlayer->getTag());NotificationCenter::getInstance()->postNotification(MSG_BLOCK_WAY_EVENT,str);return;}.............}

3. GameBaseScene. cpp registers MSG_BLOCK_WAY_EVENT message observer

void GameBaseScene::registerNotificationObserver(){...........NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(GameBaseScene::receivedNotificationOMsg),MSG_BLOCK_WAY_EVENT,NULL);............}

4. receivedNotificationOMsg processes the message and calls the doBlockWayEvent Method Based on the role.

void GameBaseScene::receivedNotificationOMsg(Object* data){...........case MSG_BLOCK_WAY_EVENT_TAG:{int playerTag = messageVector.at(3)->intValue();switch(playerTag){case PLAYER_1_TAG:{doBlockWayEvent(player1);break;}case PLAYER_2_TAG:{doBlockWayEvent(player2);break;}}break;}.............}

5. The doBlockWayEvent method is to determine whether a crab has been encountered Based on the role. If a crab has been encountered and a series of animations such as an injured player are played, after the animation is played, set the role to invisible and send the MSG_PICKONE_TOGO message, let the Controller remove the next role and continue walking.

Void GameBaseScene: doBlockWayEvent (RicherPlayer * player) {if (player-> getBoundingBox (). containsPoint (item_crab-> getPosition () + ccp (item_crab-> getContentSize (). width/2,-item_crab-> getContentSize (). height/2) {// when you encounter a crab, set the crab location to make it invisible item_crab-> setPosition (ccp (-200,-200 )); // set the random number of player-> restTimes = rand () % (5) + 1; log ("doBlockWayEvent intersetcRect "); // The ambulance changes to visible emerg-> setVisible (true); // The distance from the ambulance to the left from the right to the int distance = tableStartPosition_x-player-> getPosition (). x; // emerg-> setPosition (player-> getPosition () + ccp (distance, 0 )); // animation fog> setVisible (true); fog> setPosition (player-> getPosition (); Repeat * repeate0 = Repeat :: create (fog-> getNormal_anmi (), 2); fog-> runAction (repeate0); // MoveBy * moveBy = MoveBy: create (1.0f, ccp (-distance, 0); MoveBy * moveBy2 = MoveBy: create (0.5f, ccp (-60,0); Repeat * repeate = Repeat :: create (emerg-> getCar_go_anmi (), 5); Repeat * repeate2 = Repeat: create (emerg-> getCar_stop_anmi (), 1); Sequence * spawnAction = Sequence :: create (Spawn: create (moveBy, repeate, NULL), Spawn: create (moveBy2, repeate2, NULL), CallFunc: create (CC_CALLBACK_0 (GameBaseScene: endCarGo, this), NULL); spawnAction-> retain (); emerg-> runAction (spawnAction ); // set the role invisible if (player-> getTag () = player_shorttag) {scheduleOnce (schedule_selector (GameBaseScene: setPlayer1InVisible), 2.5f) according to the animation playback time );} else if (player-> getTag () = PLAYER_2_TAG) {scheduleOnce (schedule_selector (GameBaseScene: setPlayer2InVisible), 2.5f);} // Toast displays cococostoast :: createToast (this, String: createWithFormat ("% s % d % s", LanguageString: getInstance ()-> getLanguageString (PLAYER_HURT)-> getCString (), player-> restTimes, LanguageString: getInstance ()-> getLanguageString (RICH_DAY)-> getCString (), TOAST_SHOW_TIME, player-> getPosition ());} else {// if no crab is encountered, the role continues to send a message to handle the stay location event: whether there is a question mark time, and then whether the House in the upper and lower adjacent locations needs to be purchased, upgraded, paid, and other icationicationcenter :: getInstance ()-> postNotification (MSG_HANDLE_PROP_EVENT, String: createWithFormat ("% d", MSG_HANDLE_PROP_EVENT_TAG);} // make the role and fight smoke animation invisible void GameBaseScene :: setPlayer1InVisible (float dt) {player1-> setVisible (false); fog-> setVisible (false);} void GameBaseScene: setPlayer2InVisible (float dt) {player2-> setVisible (false); fog-> setVisible (false);} // call this function after the ambulance starts opening the animation. Play the void GameBaseScene animation of the little yellow man carrying the stretcher :: endCarGo () {strentcher-> setVisible (true); strentcher-> setPosition (emerg-> getPosition (); MoveBy * moveBy = MoveBy: create (0.5f, ccp (60,0); ScaleBy * scaleBy = ScaleBy: create (0.5f, 0.8); Repeat * repeate = Repeat: create (strentcher-> getStretcher_empty_anmi (), 1 ); scaleBy * scaleBy2 = ScaleBy: create (0.5f, 1.25); MoveBy * moveBy2 = MoveBy: create (0.5f, ccp (-60, 0); Repeat * repeate2 = Repeat :: create (strentcher-> getStretcher_full_anmi (), 1); Sequence * spawnAction = Sequence: create (Spawn: create (moveBy, scaleBy, repeate, NULL), Spawn :: create (moveBy2, scaleBy2, repeate2, NULL), CallFunc: create (CC_CALLBACK_0 (GameBaseScene: startCarGoAgain, this), NULL); spawnAction-> retain (); strentcher-> runAction (spawnAction);} // play the ambulance drive away animation void GameBaseScene: startCarGoAgain () {strentcher-> setVisible (false ); int distance = emerg-> getPositionX (); MoveBy * moveBy = MoveBy: create (1.0f, ccp (-distance, 0); Repeat * repeate = Repeat :: create (emerg-> getCar_go_anmi (), 5); Sequence * spawnAction = Sequence: create (Spawn: create (moveBy, repeate, NULL), CallFunc :: create (CC_CALLBACK_0 (GameBaseScene: endCarGoLast, this), NULL); spawnAction-> retain (); emerg-> runAction (spawnAction);} // After the ambulance leaves, call this method to make the ambulance invisible and send the MSG_PICKONE_TOGO message so that other roles can continue to walk. void GameBaseScene: endCarGoLast () {emerg-> setVisible (false); icationicationcenter :: getInstance ()-> postNotification (MSG_PICKONE_TOGO, String: createWithFormat ("% d", MSG_PICKONE_TOGO_TAG ));}

6. view the pickOnePlayerToGo method in the controller.

Void RicherGameController: pickOnePlayerToGo () {for (auto it = GameBaseScene: players_vector.begin (); it! = GameBaseScene: players_vector.end (); it ++) {RicherPlayer * richerPlayer = dynamic_cast <RicherPlayer *> (* it); // If the role takes turns to walk, if (richerPlayer-> getIsMyTurn () & richerPlayer-> restTimes> 0) {String * str = String :: createWithFormat ("% d-% f-% d", MSG_REST_TAG, 0.0f, 0.0f, richerPlayer-> getTag (); icationicationcenter: getInstance () -> postNotification (MSG_REST, str); return;} // if the role is in turn and is not resting, if (RicherPlayer-> getIsMyTurn () & richerPlayer-> restTimes = 0) {richerPlayer-> setVisible (true); // role 1 sends the MSG_GO message, show go button if (richerPlayer-> getTag () = PLAYER_1_TAG) {icationicationcenter: getInstance ()-> postNotification (MSG_GO, String: createWithFormat ("% d ", MSG_GO_SHOW_TAG); return;} // Path obtained by other roles, automatically walk int randNumber = rand () % 6 + 1; RouteNavigation: getInstance ()-> getPath (richerPlayer, randNumber, GameBaseScene:: CanPassGrid, GameBaseScene: Signature, GameBaseScene: tiledColsCount); richerPlayer-> startGo (RouteNavigation: getInstance ()-> getPathRow_vector (), RouteNavigation: getInstance () -> getPathCols_vector (); return ;}} oneRoundDone = true; // when you get here, it indicates that all the roles have finished walking. Call the resetPlayerGoTurn method, reset the role IsMyTurn to true and other resetPlayerGoTurn ();} // call this method after each round ends, so that the role's restTimes minus 1 void RicherGameController: resetPlayerGoTurn () {for (auto It = GameBaseScene: players_vector.begin (); it! = GameBaseScene: players_vector.end (); it ++) {RicherPlayer * richerPlayer = dynamic_cast <RicherPlayer *> (* it); richerPlayer-> setIsMyTurn (true ); richerPlayer-> restTimes --; if (richerPlayer-> restTimes <0) {richerPlayer-> restTimes = 0 ;}// send MSG_ROUND_COUNT to update the number of icationcenter: getInstance () -> postNotification (MSG_ROUND_COUNT, String: createWithFormat ("% d", MSG_ROUND_COUNT_TAG); // After the settings are complete, call pickOnePlayerToGo to perform the next walk pickOnePlayerToGo ();}

7. GameBaseScene. cpp registers MSG_REST message observer

void GameBaseScene::registerNotificationObserver(){...........NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(GameBaseScene::receivedNotificationOMsg),MSG_REST,NULL);.............}

8. Process MSG_REST messages
When you receive the rest message, first set IsMyTurn to false, and the toast display requires several rounds of rest. After the display ends, call the GameBaseScene: sendMSGPickOneToGO method to continue the next role.

void GameBaseScene::receivedNotificationOMsg(Object* data){..........case MSG_REST_TAG:{buy_land_x = messageVector.at(1)->floatValue();buy_land_y = messageVector.at(2)->floatValue();int playerTag = messageVector.at(3)->intValue();switch(playerTag){case PLAYER_1_TAG:{player1->setIsMyTurn(false);CocosToast::createToast(this, String::createWithFormat("%s %d %s",LanguageString::getInstance()->getLanguageString(IN_HOSPITAL_REMAIN)->getCString(),player1->restTimes,LanguageString::getInstance()->getLanguageString(RICH_DAY)->getCString())->getCString(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOneToGO);break;}case PLAYER_2_TAG:{player2->setIsMyTurn(false);CocosToast::createToast(this, String::createWithFormat("%s %d %s",LanguageString::getInstance()->getLanguageString(IN_HOSPITAL_REMAIN)->getCString(),player2->restTimes,LanguageString::getInstance()->getLanguageString(RICH_DAY)->getCString())->getCString(), TOAST_SHOW_TIME,player2->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOneToGO);break;}}break;}..........}

9. In the CocosToast. cpp file, the SEL_CallFun method function pointer is added to call the passed method, for example, GameBaseScene: sendMSGPickOneToGO.
Typedef void (Layer: * SEL_CallFun )();
Void CocosToast: createToast (cocos2d: Node * node, const std: string & msg, const float & time, Vec2 point, SEL_CallFun method );

10. If the house owner stays in the hospital after the role finishes walking, the toll will be waived. Toast prompt, and call GameBaseScene: sendMSGPickOneToGO to send the walking message

void GameBaseScene::payTolls(int payTag,float x,float y ,int playerTag){.............if(landOwner->restTimes > 0){CocosToast::createToast(this, LanguageString::getInstance()->getLanguageString(IN_HOSPITAL)->getCString(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOneToGO);return;}.............}

Part



Click to download the code


Http://download.csdn.net/detail/lideguo1979/8369525


To be continued ..................

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.