Cocos2d-x 3.2 大富翁游戏项目开发-第二十一部分 捡到积分卡 恢复体力
2015-01-18 00:21
381 查看
跟前面第18部分处理,问号事件一样,角色行走完毕后,还要检查停留问题是否有积分卡,如果有则增加相应的体力值
1、新建Item_strength_up类,该类用来显示角色体力回升的动画。
2、在RicherGameController类的handlePropEvent方法中,添加积分卡的判断,如果停留位置有积分卡,则发送增加体力的MSG_STRENGTH_UP消息
3、GameBaseScene中继续注册增加体力值的观察以及处理代码
4、体力动画播放完毕后,调用发送消息 sendMSGDealAroundLand ,来处理角色上下左右相邻地块事件
效果如图
点击下载代码
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: { ............. } } }
效果如图
点击下载代码
相关文章推荐
- Cocos2d-x 3.2 大富翁游戏项目开发-第四部分 退出对话框
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十部分 螃蟹挡路(code)
- Cocos2d-x 3.2 大富翁游戏项目开发-第十五部分 升级地块
- Cocos2d-x 3.2 大富翁游戏项目开发-第三十部分 项目移植
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市
- Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-关卡选择ScrollView
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠
- Cocos2d-x 3.2 大富翁游戏项目开发-第十七部分 Toast提示框
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十九部分 游戏配音
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1
- Cocos2d-x 3.2 大富翁游戏项目开发-第十三部分 购买空地
- Cocos2d-x 3.2 大富翁游戏项目开发-第九部分 代码部分优化
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十三部分 购买彩票
- Cocos2d-x 3.2 大富翁游戏项目开发-第二部分片头动画
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2
- Cocos2d-x 3.2 大富翁游戏项目开发-第十一部分 提升角色行走体验
- Cocos2d-x 3.2 大富翁游戏项目开发-第十二部分 显示回合计数器
- Cocos2d-x 3.2 大富翁游戏项目开发-第二十六部分 人物技能
- Cocos2d-x 3.2 大富翁游戏项目开发-第十九部分 国际化-解决中文乱码