Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,poinTinR_202_11845@ap.x,poinTinR_202_11845@ap.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:
				{
					.............
				}				
			}

}

效果如图


点击下载代码

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第二十一部分 捡到积分卡 恢复体力全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第二十一部分 捡到积分卡 恢复体力所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。