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, 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:
				{
					.............
				}				
			}

}

效果如图


点击下载代码

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值