码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-18 00:53:56      阅读:201      评论:0      收藏:0      [点我收藏+]

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

}

效果如图

技术分享

点击下载代码

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

标签:cocos2d-x

原文地址:http://blog.csdn.net/lideguo1979/article/details/42818389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!