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,x,y,_richerPlayer->getTag());
						 NotificationCenter::getInstance()->postNotification(MSG_BUY,str);
					}else
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_1_TAG,x,y,_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,x,y,_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",MSG_BUY_LAND_1_TAG,x,y,_richerPlayer->getTag());
						NotificationCenter::getInstance()->postNotification(MSG_BUY,str);
					}else
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_1_TAG,x,y,_richerPlayer->getTag());
						NotificationCenter::getInstance()->postNotification(MSG_PAY_TOLLS,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,x,y,_richerPlayer->getTag());
						NotificationCenter::getInstance()->postNotification(MSG_BUY,str);
					}else
					{
						String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_2_TAG,x,y,_richerPlayer->getTag());
						NotificationCenter::getInstance()->postNotification(MSG_PAY_TOLLS,str);
					}
					break;
			}
		 if(titleId == GameBaseScene::player2_building_3_tiledID)<span style="font-family: Arial, Helvetica, sans-serif;">//如果是角色2的等级为3的地块</span>
			{
				if(_richerPlayer->getTag() != PLAYER_2_TAG)
				{
					String * str = String::createWithFormat("%d-%f-%f-%d",MSG_PAY_TOLLS_3_TAG,x,y,_richerPlayer->getTag());
					NotificationCenter::getInstance()->postNotification(MSG_PAY_TOLLS,str);
				}else
				{
				NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",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,String::createWithFormat("%d",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,x,y,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,buy_land_x,buy_land_y,playerTag);
			break;
		}
case MSG_PAY_TOLLS_2_TAG:
		……………………………….
case MSG_PAY_TOLLS_3_TAG:
		…………………………………..
}
void GameBaseScene::payTolls(int payTag,float x,float y ,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,y),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,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG));
			break;
		}
		case PLAYER_2_TAG:
		{
			refreshMoneyLabel(player2,-2000);
			NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",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

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

时间: 2024-08-07 11:06:12

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

Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市

当角色走到股市图标时,进入股市界面.每走完一个回合,增加一条股票数据, 股市界面上半部分显示股票信息,包括代码,名称,当前价格,买入价格,涨跌百分比,角色持有的股票数量 下半部分显示股票价格走势,当点击一个股票时,显示相关股票的价格走势,共显示最新14条的价格走势. 每次点击购买,买入100股 .点击卖出,则卖出所持有的该股的所有股票.成交价格 等信息动态更新 点击返回,返回到游戏主界面,并更新角色资金值 1.首先添加股票类 包括代码,名称,买入价格,涨跌百分比,持仓数量等定义以及相关的get

Cocos2d-x 3.2 大富翁游戏项目开发-第十六部分 相连地块缴纳过路费

当走到其他角色的地块时,根据当前地块是否连片,连片的话统一计算需要缴纳的过路费,连片的计算方式,是各个地块过路费总和. 首先获取当前行走角色路过的地块的x y坐标(Land layer层的坐标),然后获取角色的坐标(GL的坐标,需要转换成map中的坐标), 然后对这2个坐标值进行横向和纵向比较. 如图A位置:以寻找左边地块的方法为例分析 1. 把角色A坐标转换成map中的坐标 2. 把当前0号地块的sprite对象放入容器中,以便后面播放淡入淡出的动画 3. 0号地块x坐标同角色A的map坐标的

Cocos2d-x 3.2 大富翁游戏项目开发-第十部分实现人物轮流行走

现在只能让我方角色行走,对手并没有行动,如何实现轮流行走呢? 有2种方法,一种是采用刷帧控制器在update进行,另一种采用消息机制的形式实现,我们尽量避免使用刷帧控制器.就采用第二种消息机制吧 我们在GameBaseScene中定义变量players_vector [Player*] 来存放所有的角色: 在添加新对象时,给该对象设置属性_isMyTurn为true 这样所有对象添加完毕后players_vector [Player*]所有的isMyTurn都为true,表示可以调用go方法,进

Cocos2d-x 3.2 大富翁游戏项目开发-第二十九部分 游戏配音

我从大富翁里提取出来里面的wav音效文件,放到我们的游戏中以增加趣味性,仅供学习研究之用 1.修改AppDelegate.cpp文件 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stop

Cocos2d-x 3.2 大富翁游戏项目开发-第二十六部分 人物技能

本节主要是添加人物技能,技能包括 暴风骤雨:此技能可以把地块变成空白地块, 随心步:  选择想走的步数,想走几步走几步 巧取豪夺:把对手的土地变成自己的 技能包含的信息:等级 和 耗费体力 等级共5级,每提高一个等级,需要耗费的体力值减10 暴风骤雨:初始耗费体力值70 随心步:  初始耗费体力值50 巧取豪夺:初始耗费体力值100 1.添加技能SkillCard 类,包含了技能基本信息以及如何显示技能 其实现和前面讲解的股票 彩票 card类相似,不再累述 2.修改RicherPlayer类,

Cocos2d-x 3.2 大富翁游戏项目开发-第二十部分 螃蟹挡路

该部分我们添加螃蟹伤人事件,道路位置随机添加螃蟹精灵,当角色行走完毕如果停留位置碰到了螃蟹,首先播放伤人动画,然后是播放救护车把角色带走动画. 如果轮流到该角色行走时,吐司提示住院还有几天,当住院天数到期,该角色才可以继续行走 新建了几个精灵类 Item_crab.cpp 螃蟹 Item_emergency.cpp 救护车 Item_fog.cpp 烟雾 Item_stretcher.cpp 担架 //该方法在场景中添加这几个精灵 void GameBaseScene::initItemSpri

Cocos2d-x 3.2 大富翁游戏项目开发-第二十二部分 拾到珍珠

该部分同前面处理逻辑基本一样,道路上随机出现闪烁的珍珠,在捡到珍珠后,toast显示捡到珍珠资金增加. 1.编写Item_ball类,该类存储了珍珠相关动画 bool Item_ball::init() { addItemSpriteFrameCache(); SpriteFrame* spf; spf = itemSpriteFrameCache->getSpriteFrameByName("ball_01.png"); Sprite::initWithSpriteFrame(

Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖

每隔N个回合,彩票开奖一次,每期开奖奖金固定5万,暂不累积.摇奖效果一般,以后考虑用物理引擎实现 1.定义彩票开奖类 bool LotteryPublish::init() { addItemSpriteFrameCache(); SpriteFrame* spf; spf = itemSpriteFrameCache->getSpriteFrameByName("publish_ly01.png"); Sprite::initWithSpriteFrame(spf); setI

Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件

角色走完要求的步数后,先查看停留位置是否有问号,如果有,先处理问号事件,处理完毕后,再处理相邻周边上下左右地块问题,购买.升级或缴纳过路费. 问号随机事件主要是如下事件: 政府鼓励投资,返还税金10000 政府严查账务,补交税金20000 喝到假酒,上吐下泻,体力耗光 吃了大补丸,体力恢复 投资获利,分红20000 投资失败,亏损30000 由于还没有进行国际化处理,在用中文的时候,会出现乱码,我们暂时先都用英文处理一下,后期统一做国际化处理 1.与之对应的变量定义在ConstUtil.h文件中