Cocos2d-x 3.2 大富翁遊戲項目開發-第二十一部分 撿到積分卡 恢複體力,cocos2d-x項目開發

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第二十一部分 撿到積分卡 恢複體力,cocos2d-x項目開發

跟前面第18部分處理,問號事件一樣,角色行走完畢後,還要檢查停留問題是否有積分卡,如果有則增加相應的體力值

1、建立Item_strength_up類,該類用來顯示角色體力回升的動畫。

void Item_strength_up::addItemSpriteFrameCache(){itemSpriteFrameCache = SpriteFrameCache::getInstance();itemSpriteFrameCache->addSpriteFramesWithFile("images/strength_up.plist","images/strength_up.png");memset(name, 0, 20);for (int i=1; i<=14; i++) {sprintf(name, "strength_up_%02d.png",i);item_anim_vector.pushBack(itemSpriteFrameCache->getSpriteFrameByName(name));}}void Item_strength_up::setItemAnimate(){if(!AnimationCache::getInstance()->getAnimation("strength_up_animation")){AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(item_anim_vector,0.1f),"strength_up_animation");} normal_anmi = Animate::create(AnimationCache::getInstance()->getAnimation("strength_up_animation")); normal_anmi->retain();}

2、在RicherGameController類的handlePropEvent方法中,添加積分卡的判斷,如果停留位置有積分卡,則發送增加體力的MSG_STRENGTH_UP訊息

void RicherGameController::handlePropEvent(){......... if(endId == GameBaseScene::strength_30_tiledID) { String * str = String::createWithFormat("%d-%f-%f-%d",MSG_STRENGTH_UP30_TAG,pointInMap.x,pointInMap.y,_richerPlayer->getTag()); NotificationCenter::getInstance()->postNotification(MSG_STRENGTH_UP,str); return; } if(endId == GameBaseScene::strength_50_tiledID) { String * str = String::createWithFormat("%d-%f-%f-%d",MSG_STRENGTH_UP50_TAG,pointInMap.x,pointInMap.y,_richerPlayer->getTag()); NotificationCenter::getInstance()->postNotification(MSG_STRENGTH_UP,str); return; } if(endId == GameBaseScene::strength_80_tiledID) { String * str = String::createWithFormat("%d-%f-%f-%d",MSG_STRENGTH_UP80_TAG,pointInMap.x,pointInMap.y,_richerPlayer->getTag()); NotificationCenter::getInstance()->postNotification(MSG_STRENGTH_UP,str); return; }}

3、GameBaseScene中繼續註冊增加體力值的觀察以及處理代碼

void GameBaseScene::registerNotificationObserver(){.............NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(GameBaseScene::receivedNotificationOMsg),MSG_STRENGTH_UP,NULL);}receivedNotificationOMsg調用doItemStrengthUp方法,來具體處理體力值增加和體力恢複動畫的播放void GameBaseScene::receivedNotificationOMsg(Object* data){.............case MSG_STRENGTH_UP30_TAG:{doItemStrengthUp(MSG_STRENGTH_UP30_TAG, messageVector.at(3)->intValue());break;}case MSG_STRENGTH_UP50_TAG:{doItemStrengthUp(MSG_STRENGTH_UP50_TAG, messageVector.at(3)->intValue());break;}case MSG_STRENGTH_UP80_TAG:{doItemStrengthUp(MSG_STRENGTH_UP80_TAG, messageVector.at(3)->intValue());break;}}

4、體力動畫播放完畢後,調用發送訊息 sendMSGDealAroundLand ,來處理角色上下左右相鄰地塊事件

void GameBaseScene::doItemStrengthUp(int strengthUp,int playerTag){int strengthValue = 0;//根據積分卡值,給strengthValue賦相應值switch(strengthUp){case MSG_STRENGTH_UP30_TAG:{strengthValue =30;break;}case MSG_STRENGTH_UP50_TAG:{strengthValue =50;break;}case MSG_STRENGTH_UP80_TAG:{strengthValue =80;break;}}switch(playerTag){case PLAYER_1_TAG:{//設定item_strength_up在角色的位置可見,播放動畫,顯示toast。播放完畢設定item_strength_up不可見item_strength_up->setVisible(true);item_strength_up->setPosition(player1->getPosition()-ccp(5,0));Sequence* action = Sequence::create(item_strength_up->getNormal_anmi(),CallFunc::create([this](){item_strength_up->setVisible(false);}),NULL);item_strength_up->runAction(action);CocosToast::createToast(this, String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(STRENGTH_UP)->getCString(),strengthValue)->getCString(),TOAST_SHOW_TIME,player1->getPosition());refreshStrengthLabel(player1,strengthValue);scheduleOnce(schedule_selector( GameBaseScene::sendMSGDealAroundLand),3.0f);break;}case PLAYER_2_TAG:{.............}}}

效果


點擊下載代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.