Cocos2d-x 3.2 大富翁游戏项目开发-第二十一部分 捡到积分卡 恢复体力

跟前面第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,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,str);
		 return;
	 }

	 if(endId == GameBaseScene::strength_80_tiledID)
	 {
		 String * str = String::createWithFormat("%d-%f-%f-%d",MSG_STRENGTH_UP80_TAG,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:
				{
					.............
				}				
			}

}

效果如图


点击下载代码

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...