Cocos2d-x 3.2 大富翁游戏项目开发-第十五部分 升级地块

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第十五部分 升级地块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当路过的地块属于自己的时,如果是第一角色则弹出对话框,提示升级地块,其他角色直接升级地块。

当路过的地块不是自己的时,需要缴纳与地块等级相应的过路费。

修改RicherGameController.cpp文件的handlePropEvent()方法

针对不同的地块,发送不同的消息

void RicherGameController::handlePropEvent()
{
 …………………………
for (int i = 0; i < 4; i++) //遍历角色上下左右相邻位置的地块
	 {
		 Point ptMap = Util::GL2map(Vec2(positionAroundEnd[i][0],positionAroundEnd[i][1]),GameBaseScene::_map);
		 const int titleId = GameBaseScene::landLayer->getTileGIDAt(ptMap);

		 float x = ptMap.x;
		 float y = ptMap.y;
		 if(titleId == GameBaseScene::blank_land_tiledID)//如果是空地,发送购买消息
			{

					String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BUY_BLANK_TAG,x,y,_richerPlayer->getTag());
					NotificationCenter::getInstance()->postNotification(MSG_BUY,str);
					break;
			}
		 if(titleId == GameBaseScene::player1_building_1_tiledID)//如果是角色1的等级为1的地块
			{
					if(_richerPlayer->getTag() == PLAYER_1_TAG)//角色1则 发送购买消息,否则发送缴纳过路费消息
					{
						 String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BUY_LAND_1_TAG,_richerPlayer->getTag());
						 NotificationCenter::getInstance()->postNotification(MSG_BUY,str);			
					}else
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_1_TAG,_richerPlayer->getTag());
						NotificationCenter::getInstance()->postNotification(MSG_PAY_TOLLS,str);	
					}
					
					break;
			}
		 if(titleId == GameBaseScene::player1_building_2_tiledID)
			{
					…………………//同上
			}	

		 if(titleId == GameBaseScene::player1_building_3_tiledID)
			{
				if(_richerPlayer->getTag() != PLAYER_1_TAG) //如果是角色1的等级为3的地块,当前路过的角色不是角色1则发送缴纳过路费消息
				{
					String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_3_TAG,_richerPlayer->getTag());
					NotificationCenter::getInstance()->postNotification(MSG_PAY_TOLLS,str);	
				}else  //当路过的角色是角色1,则角色1什么都不做,直接发送消息,进行下一回合行走
				{
				NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG));
				}
					break;
			}

		 if(titleId == GameBaseScene::player2_building_1_tiledID) //如果是角色2的等级为1的地块
			{
					if(_richerPlayer->getTag() == PLAYER_2_TAG)
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",_richerPlayer->getTag());				
						NotificationCenter::getInstance()->postNotification(MSG_BUY,str);	
					}
					break;
			}
		 if(titleId == GameBaseScene::player2_building_2_tiledID)<span style="font-family: Arial,Helvetica,sans-serif;">//如果是角色2的等级为2的地块</span>
			{
					if(_richerPlayer->getTag() == PLAYER_2_TAG)
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BUY_LAND_2_TAG,MSG_PAY_TOLLS_2_TAG,str);	
					}
					break;
			}
		 if(titleId == GameBaseScene::player2_building_3_tiledID)<span style="font-family: Arial,sans-serif;">//如果是角色2的等级为3的地块</span>
			{
				if(_richerPlayer->getTag() != PLAYER_2_TAG)
				{
					String * str = String::createWithFormat("%d-%f-%f-%d",str);			
				}else
				{
				NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,MSG_PICKONE_TOGO_TAG));
				}
				break;
			}
		 
	 }


}

修改一下GameBaseScene.cpp注册并处理相关消息

void GameBaseScene::registerNotificationObserver()
{
	………………..
//新添缴纳过路费消息观察
	NotificationCenter::getInstance()->addObserver(
		this,callfuncO_selector(GameBaseScene::receivedNotificationOMsg),MSG_PAY_TOLLS,NULL);	
}

void GameBaseScene::receivedNotificationOMsg(Object* data)
{
………………………..
case MSG_BUY_BLANK_TAG:
…………………
case MSG_BUY_LAND_1_TAG://升级地块等级,脚印地块,升级为海星地块
			buy_land_x = messageVector.at(1)->floatValue();
			buy_land_y = messageVector.at(2)->floatValue();
			int playerTag = messageVector.at(3)->intValue();
			
			switch(playerTag)
			{
				case PLAYER_1_TAG:
				{
					showBuyLandDialog(MSG_BUY_LAND_1_TAG);
					break;
				}
				case PLAYER_2_TAG:
				{
<span style="white-space:pre">				</span> //升级地块的方法	
                                 buyLand(MSG_BUY_LAND_1_TAG,buy_land_x,buy_land_y,starFish2Sprite,player2_building_2_tiledID,player2,PLAYER2_1_PARTICLE_PLIST);
				 NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,MSG_PICKONE_TOGO_TAG));
					break;
				}				
			}
			break;
		}
case MSG_BUY_LAND_2_TAG://海星地块升级为心形地块,方法同上
……………………………


}


void GameBaseScene::buyLand(int buyTag,float x,float y,Sprite* landSprite,int landLevel,RicherPlayer* player,char* particlelistName)
{
	       int money =0;
	//根据升级地块类型,确定需要花费的资金
	        if(buyTag == MSG_BUY_BLANK_TAG)
			{
				money = LAND_BLANK_MONEY;				
			}
 if(buyTag == MSG_BUY_LAND_1_TAG)
			{
				money = LAND_LEVEL_1_MONEY;
			}
	      if(buyTag == MSG_BUY_LAND_2_TAG)
			{
				money = LAND_LEVEL_2_MONEY;
			}
	//播放动画和粒子效果,然后更新资金的Label
			Point pointOfGL = Util::map2GL(ccp(x,y),GameBaseScene::_map);
					
			landSprite->setVisible(true);
			landSprite->setPosition(pointOfGL);
			Point pointOfMap = ccp(x,y);
landSprite->runAction(Sequence::create(scaleby1ForBuyLand,scaleby2ForBuyLand,CallFunc::create([this,pointOfMap,pointOfGL,landSprite,landLevel,player,money,particlelistName]()
				{
					playParticle(pointOfGL,particlelistName);
					landSprite->setVisible(false);
					landLayer->setTileGID(landLevel,ccp(x,y));
					refreshMoneyLabel (player,-money);				
				}),NULL));

}

继续查看receivedNotificationOMsg()方法下面的分支
void GameBaseScene::receivedNotificationOMsg(Object* data)
{
……………………. //当收到缴纳过路费的消息时
case MSG_PAY_TOLLS_1_TAG://当缴纳等级为1地块的过路费时,相应参数传到payTolls方法
		{
			buy_land_x = messageVector.at(1)->floatValue();
			buy_land_y = messageVector.at(2)->floatValue();
			int playerTag = messageVector.at(3)->intValue();
			payTolls(MSG_PAY_TOLLS_1_TAG,playerTag);
			break;
		}
case MSG_PAY_TOLLS_2_TAG:
		……………………………….
case MSG_PAY_TOLLS_3_TAG:
		…………………………………..
}

void GameBaseScene::payTolls(int payTag,int playerTag)
{
	//根据消息类型,确定缴纳过路费金额
	int money =0;
	  if(payTag == MSG_PAY_TOLLS_1_TAG)
		{
			money = LAND_BLANK_MONEY;
		}
  if(payTag == MSG_PAY_TOLLS_2_TAG)
		{
			money = LAND_LEVEL_1_MONEY;
		}
	if(payTag == MSG_PAY_TOLLS_3_TAG)
		{
			money = LAND_LEVEL_2_MONEY;
		}
	//路过的地块做一个淡出淡入的动画,然后更新相应资金以及label
	Point pointOfGL = Util::map2GL(ccp(x,GameBaseScene::_map);
	Sprite* sp = landLayer->getTileAt(ccp(x,y));
					
	sp->runAction(Sequence::create(landFadeOut,landFadeIn,NULL));

	RicherPlayer* landOwner = getPlayerByTiled(buy_land_x,buy_land_y);
	refreshMoneyLabel(landOwner,money);
	switch(playerTag)
	{
		case PLAYER_1_TAG:
		{
			refreshMoneyLabel(player1,-money);
			NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,MSG_PICKONE_TOGO_TAG));
			break;
		}
		case PLAYER_2_TAG:
		{
			refreshMoneyLabel(player2,-2000);
			NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,MSG_PICKONE_TOGO_TAG));
			break;
		}				
	}
}

把海星 、心形的精灵,以及淡入淡出动画初始化放在了doSomeForParticle方法
void GameBaseScene::doSomeForParticle()
{
	landFadeOut = FadeOut::create(0.1);
	landFadeIn = FadeIn::create(0.1);
	landFadeOut->retain();
	landFadeIn->retain();	
	………………………..
	starFish1Sprite = Sprite::create(PLAYER1_2_PARTICLE_PNG);
	addChild(starFish1Sprite);
	starFish1Sprite->setAnchorPoint(ccp(0,0));
	starFish2Sprite = Sprite::create(PLAYER2_2_PARTICLE_PNG);
	addChild(starFish2Sprite);
	starFish2Sprite->setAnchorPoint(ccp(0,0));
	heart1Sprite = Sprite::create(PLAYER1_3_PARTICLE_PNG);
	addChild(heart1Sprite);
	heart1Sprite->setAnchorPoint(ccp(0,0));
	heart2Sprite = Sprite::create(PLAYER2_3_PARTICLE_PNG);
	addChild(heart2Sprite);
	heart2Sprite->setAnchorPoint(ccp(0,0));


}

逻辑比较简单 ,大体流程如图所示:




地图升级效果




点击下载代码

http://download.csdn.net/detail/lideguo1979/8326817


未完待续.....................

原文链接:https://www.f2er.com/cocos2dx/345108.html

猜你在找的Cocos2d-x相关文章