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

该部分我们添加螃蟹伤人事件,道路位置随机添加螃蟹精灵,当角色行走完毕如果停留位置碰到了螃蟹,首先播放伤人动画,然后是播放救护车把角色带走动画。
如果轮流到该角色行走时,吐司提示住院还有几天,当住院天数到期,该角色才可以继续行走

新建了几个精灵类

Item_crab.cpp 螃蟹
Item_emergency.cpp 救护车
Item_fog.cpp 烟雾

Item_stretcher.cpp 担架


//该方法在场景中添加这几个精灵
void GameBaseScene::initItemSprite()
{
	item_crab = (Item_crab*)Item::create(ITEM_CRAB);
	addChild(item_crab);
	item_crab->runAction(RepeatForever::create(item_crab->getnormal_anmi()));
	item_crab->setVisible(false);


	emerg = Item_emergency::create();
    addChild(emerg);
	emerg->setVisible(false);

	fog = Item_fog::create();
	addChild(fog);
	fog->setVisible(false);

	strentcher = Item_stretcher::create();
	addChild(strentcher);
	strentcher->setVisible(false);
	strentcher->setAnchorPoint(ccp(0,0.9));
}

定义了定时器,每隔100秒调用方法,更新螃蟹位置

void GameBaseScene::registerBlockWaySchedule()
{
	schedule(schedule_selector(GameBaseScene::updateBlockWaySprites),100.0f);
}

void GameBaseScene::updateBlockWaySprites(float dt)
{
	int _rand1 = rand()%(wayLayerPass_vector.size()); 
	Vec2 position = wayLayerPass_vector.at(_rand1);
	position.x -= 5;
	position.y +=tiledHeight; 
	item_crab->setVisible(true);
	item_crab->setPosition(position);
	item_crab->setAnchorPoint(ccp(0,0.6));
}

1、首先在RicherPlayer.cpp中,添加 restTimes 属性,记录受伤后,角色需要住院停留的回合数
2、修改void RicherGameController::endGo()方法
当角色行走完毕,发送MSG_BLOCK_WAY_EVENT消息,该消息用来处理行走完毕后,检查道路上是否存在螃蟹等
void RicherGameController::endGo()
{
	...........
	if(stepHasGone >= stepsCount)
	{

		String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BLOCK_WAY_EVENT_TAG,0.0f,_richerPlayer->getTag());
		NotificationCenter::getInstance()->postNotification(MSG_BLOCK_WAY_EVENT,str);
		return;
	}
	.............
}

3、GameBaseScene.cpp注册MSG_BLOCK_WAY_EVENT消息观察者
void GameBaseScene::registerNotificationObserver()
{
	...........
		NotificationCenter::getInstance()->addobserver(
		this,callfuncO_selector(GameBaseScene::receivednotificationOMsg),MSG_BLOCK_WAY_EVENT,NULL);
	............

}

4、receivednotificationOMsg 具体处理该消息,根据角色调用doblockWayEvent 方法
void GameBaseScene::receivednotificationOMsg(Object* data)
{
...........
	case MSG_BLOCK_WAY_EVENT_TAG:
		{
			int playerTag = messageVector.at(3)->intValue();
			switch(playerTag)
			{
				case PLAYER_1_TAG:
				{
					doblockWayEvent(player1);
					break;
				}
				case PLAYER_2_TAG:
				{
					doblockWayEvent(player2);
					break;
				}				
			}
			
			break;
		}
.............

}

5、doblockWayEvent方法,就是根据角色判断是否碰到了螃蟹,如果碰到了,播放受伤等一系列动画,动画播放完毕后,设置角色不可见,并发送MSG_PICKONE_TOGO消息,让控制器取出下一个角色继续行走。
void GameBaseScene::doblockWayEvent(RicherPlayer* player)
{
	if(player->getBoundingBox().containsPoint(item_crab->getPosition()+ccp(item_crab->getContentSize().width/2,-item_crab->getContentSize().height/2)))
	{
			//碰到了螃蟹,设置螃蟹位置,让其不可见
			item_crab->setPosition(ccp(-200,-200));
			//设置角色随机停留回合数
			player->restTimes =  rand()%(5) + 1;;
			log("doblockWayEvent intersetcRect");
			//救护车变为可见
			emerg->setVisible(true);
			//救护车从右往左移动的距离
			int distance = tableStartPosition_x-player->getPosition().x;
			//救护车的位置
			emerg->setPosition(player->getPosition()+ccp(distance,0));
			//螃蟹和角色打架的烟雾动画
			fog->setVisible(true);
			fog->setPosition(player->getPosition());
			Repeat* repeate0 = Repeat::create(fog->getnormal_anmi(),2);
			fog->runAction(repeate0);
			//救护车的移动动画
			MoveBy* moveBy = MoveBy::create(1.0f,ccp(-distance,0));
			MoveBy* moveBy2 = MoveBy::create(0.5f,ccp(-60,0));
			Repeat* repeate = Repeat::create(emerg->getCar_go_anmi(),5);
			Repeat* repeate2 = Repeat::create(emerg->getCar_stop_anmi(),1);

			Sequence* spawnAction = Sequence::create(Spawn::create(moveBy,repeate,NULL),Spawn::create(moveBy2,repeate2,CallFunc::create(CC_CALLBACK_0(GameBaseScene::endCarGo,this)),NULL);
			spawnAction->retain();
			emerg->runAction(spawnAction);
			//根据动画播放的时间设置角色不可见
			if(player->getTag() == PLAYER_1_TAG)
			{
				scheduleOnce(schedule_selector(GameBaseScene::setPlayer1InVisible),2.5f);	
			}else if(player->getTag() == PLAYER_2_TAG)
			{
				scheduleOnce(schedule_selector(GameBaseScene::setPlayer2InVisible),2.5f);	
			}
			//Toast显示角色受伤住院天数信息
			CocosToast::createtoast(this,String::createWithFormat("%s %d %s",LanguageString::getInstance()->getLanguageString(PLAYER_HURT)->getCString(),player->restTimes,LanguageString::getInstance()->getLanguageString(RICH_DAY)->getCString())->getCString(),TOAST_SHOW_TIME,player->getPosition());
	}
	else
	{
		//如果没有碰到螃蟹,角色继续发送消息用于处理停留位置事件:是否有问号时间,然后是上下左右相邻位置房屋是否需要购买升级缴纳过路费等
		NotificationCenter::getInstance()->postNotification(MSG_HANDLE_PROP_EVENT,String::createWithFormat("%d",MSG_HANDLE_PROP_EVENT_TAG));
	}
}

//让角色和打架烟雾动画不可见
void GameBaseScene::setPlayer1InVisible(float dt)
{
	player1->setVisible(false);
	fog->setVisible(false);
}

void GameBaseScene::setPlayer2InVisible(float dt)
{
	player2->setVisible(false);
	fog->setVisible(false);
}
//救护车开来动画结束后调用函数,播放小黄人抬担架动画
void GameBaseScene::endCarGo()
{
	strentcher->setVisible(true);
	strentcher->setPosition(emerg->getPosition());
	MoveBy* moveBy = MoveBy::create(0.5f,ccp(60,0));
	ScaleBy* scaleBy = ScaleBy::create(0.5f,0.8);
	Repeat* repeate = Repeat::create(strentcher->getStretcher_empty_anmi(),1);
	ScaleBy* scaleBy2 = ScaleBy::create(0.5f,1.25);
	MoveBy* moveBy2 = MoveBy::create(0.5f,0));
	Repeat* repeate2 = Repeat::create(strentcher->getStretcher_full_anmi(),1);
	Sequence* spawnAction = Sequence::create(Spawn::create(moveBy,scaleBy,scaleBy2,CallFunc::create(CC_CALLBACK_0(GameBaseScene::startCarGoAgain,NULL);
	spawnAction->retain();
	strentcher->runAction(spawnAction);
}

//小黄人抬担架动画播放完毕后,播放救护车开走动画
void GameBaseScene::startCarGoAgain()

{
	strentcher->setVisible(false);
	int distance = emerg->getPositionX();
	MoveBy* moveBy = MoveBy::create(1.0f,0));
	Repeat* repeate = Repeat::create(emerg->getCar_go_anmi(),5);
	Sequence* spawnAction = Sequence::create(Spawn::create(moveBy,CallFunc::create(CC_CALLBACK_0(GameBaseScene::endCarGoLast,NULL);
	spawnAction->retain();
	emerg->runAction(spawnAction);
}

//救护车开走后,调用方法,让救护车不可见,并发送MSG_PICKONE_TOGO消息,让其他角色继续行走
void GameBaseScene::endCarGoLast()

{
	emerg->setVisible(false);
	NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,MSG_PICKONE_TOGO_TAG));
}

6、查看控制器中pickOnePlayerToGo方法
void RicherGameController::pickOnePlayerToGo()
{
	for(auto it=GameBaseScene::players_vector.begin();it!=GameBaseScene::players_vector.end();it++)
		{
			RicherPlayer* richerPlayer = dynamic_cast<RicherPlayer*>(*it);
			//如果轮流到该角色行走,但是在受伤休息,则发送MSG_REST 休息消息
			if(richerPlayer->getIsMyTurn() && richerPlayer->restTimes > 0 )
			{
				String * str = String::createWithFormat("%d-%f-%f-%d",MSG_REST_TAG,richerPlayer->getTag());
				NotificationCenter::getInstance()->postNotification(MSG_REST,str);
				return;
			}
			//如果轮流到角色行走,并且不在休息
			if(richerPlayer->getIsMyTurn() && richerPlayer->restTimes == 0)
			{
				richerPlayer->setVisible(true);
				//角色1 发送MSG_GO消息,显示go按钮
				if(richerPlayer->getTag() == PLAYER_1_TAG )
				{
					NotificationCenter::getInstance()->postNotification(MSG_GO,MSG_GO_SHOW_TAG));
					return;
				}
				//其他角色 获取路径,自动行走
				int randNumber = rand()%6 + 1; 
				RouteNavigation::getInstance()->getPath(richerPlayer,randNumber,GameBaseScene::canPassGrid,GameBaseScene::tiledRowsCount,GameBaseScene::tiledColsCount);
				richerPlayer->startGo(RouteNavigation::getInstance()->getPathRow_vector(),RouteNavigation::getInstance()->getPathCols_vector());
				return;
			}

		}
	oneRoundDone = true;
	//走到这里,说明角色都行走完毕,调用resetPlayerGoTurn方法,重新设置角色IsMyTurn为true等
	resetPlayerGoTurn();
	
}

//每一个回合结束后调用方法,让角色的restTimes 减1 
void RicherGameController::resetPlayerGoTurn()
{
	for(auto it=GameBaseScene::players_vector.begin();it!=GameBaseScene::players_vector.end();it++)
	{
		RicherPlayer* richerPlayer = dynamic_cast<RicherPlayer*>(*it);
		richerPlayer->setIsMyTurn(true);

		richerPlayer->restTimes--;
		if(richerPlayer->restTimes < 0)
		{
			richerPlayer->restTimes = 0;
		}

	}
	//发送MSG_ROUND_COUNT 更新回合数
	NotificationCenter::getInstance()->postNotification(MSG_ROUND_COUNT,MSG_ROUND_COUNT_TAG));
	//设置完毕,继续调用pickOnePlayerToGo ,进行下一轮行走
	pickOnePlayerToGo();
}

7、GameBaseScene.cpp注册MSG_REST消息观察者
void GameBaseScene::registerNotificationObserver()
{
...........
		NotificationCenter::getInstance()->addobserver(
		this,MSG_REST,NULL);
.............
}

8、处理MSG_REST消息
当收到休息的消息,首先设置IsMyTurn 为false,并吐司显示还需要休息几个回合,显示结束后调用GameBaseScene::sendMSGPickOnetoGO 方法,继续下一个角色行走
void GameBaseScene::receivednotificationOMsg(Object* data)
{
..........
	case MSG_REST_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:
				{
					player1->setIsMyTurn(false);									
					CocosToast::createtoast(this,LanguageString::getInstance()->getLanguageString(IN_HOSPITAL_REMAIN)->getCString(),player1->restTimes,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOnetoGO);				
					break;
				}
				case PLAYER_2_TAG:
				{
					player2->setIsMyTurn(false);
					CocosToast::createtoast(this,player2->restTimes,player2->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOnetoGO);
					break;
				}				
			}
			
			break;
		}
..........
}

9、CocosToast.cpp 文件添加了SEL_CallFun method 函数指针,用来调用传进来的方法,如 GameBaseScene::sendMSGPickOnetoGO 发送行走消息。
typedef void (Layer::*SEL_CallFun)();
void CocosToast::createtoast(cocos2d::Node *node,const std::string &msg,const float &time,Vec2 point,SEL_CallFun method);

10、当角色行走完毕 停留位置的房屋所有者如果在住院,则免缴过路费。吐司提示,并调用GameBaseScene::sendMSGPickOnetoGO 发送行走消息

void GameBaseScene::payTolls(int payTag,float x,float y,int playerTag)
{
.............
	if(landOwner->restTimes > 0)
			{
				CocosToast::createtoast(this,LanguageString::getInstance()->getLanguageString(IN_HOSPITAL)->getCString(),(SEL_CallFun)&GameBaseScene::sendMSGPickOnetoGO);
				return;
			}
.............
}

部分效果



点击下载代码


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


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

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...