该部份我们添加螃蟹伤人事件,道路位置随机添加螃蟹精灵,当角色行走终了如果停留位置碰到了螃蟹,首先播放伤人动画,然后是播放救护车把角色带走动画。
如果轮番到该角色行走时,吐司提示住院还有几天,当住院天数到期,该角色才可以继续行走
新建了几个精灵类
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,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方法,就是根据角色判断是不是碰到了螃蟹,如果碰到了,播放受伤等1系列动画,动画播放终了后,设置角色不可见,并发送MSG_PICKONE_TOGO消息,让控制器取出下1个角色继续行走。
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(⑵00,⑵00));
//设置角色随机停留回合数
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(⑹0,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,NULL),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,ccp(⑹0,0));
Repeat* repeate2 = Repeat::create(strentcher->getStretcher_full_anmi(),1);
Sequence* spawnAction = Sequence::create(Spawn::create(moveBy,scaleBy,repeate,NULL),Spawn::create(moveBy2,scaleBy2,repeate2,NULL),CallFunc::create(CC_CALLBACK_0(GameBaseScene::startCarGoAgain, this)),NULL);
spawnAction->retain();
strentcher->runAction(spawnAction);
}
//小黄人抬担架动画播放终了后,播放救护车开走动画
void GameBaseScene::startCarGoAgain()
{
strentcher->setVisible(false);
int distance = emerg->getPositionX();
MoveBy* moveBy = MoveBy::create(1.0f,ccp(-distance,0));
Repeat* repeate = Repeat::create(emerg->getCar_go_anmi(),5);
Sequence* spawnAction = Sequence::create(Spawn::create(moveBy,repeate,NULL),CallFunc::create(CC_CALLBACK_0(GameBaseScene::endCarGoLast, this)),NULL);
spawnAction->retain();
emerg->runAction(spawnAction);
}
//救护车开走后,调用该方法,让救护车不可见,并发送MSG_PICKONE_TOGO消息,让其他角色继续行走
void GameBaseScene::endCarGoLast()
{
emerg->setVisible(false);
NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",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,0.0f,0.0f,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,String::createWithFormat("%d",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,String::createWithFormat("%d",MSG_ROUND_COUNT_TAG));
//设置终了,继续调用pickOnePlayerToGo ,进行下1轮行走
pickOnePlayerToGo();
}
7、GameBaseScene.cpp注册MSG_REST消息视察者
void GameBaseScene::registerNotificationObserver()
{
...........
NotificationCenter::getInstance()->addObserver(
this,
callfuncO_selector(GameBaseScene::receivedNotificationOMsg),
MSG_REST,
NULL);
.............
}
8、处理MSG_REST消息
当收到休息的消息,首先设置IsMyTurn 为false,并吐司显示还需要休息几个回合,显示结束后调用GameBaseScene::sendMSGPickOneToGO 方法,继续下1个角色行走
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, String::createWithFormat("%s %d %s",LanguageString::getInstance()->getLanguageString(IN_HOSPITAL_REMAIN)->getCString(),player1->restTimes,LanguageString::getInstance()->getLanguageString(RICH_DAY)->getCString())->getCString(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOneToGO);
break;
}
case PLAYER_2_TAG:
{
player2->setIsMyTurn(false);
CocosToast::createToast(this, String::createWithFormat("%s %d %s",LanguageString::getInstance()->getLanguageString(IN_HOSPITAL_REMAIN)->getCString(),player2->restTimes,LanguageString::getInstance()->getLanguageString(RICH_DAY)->getCString())->getCString(), TOAST_SHOW_TIME,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(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGPickOneToGO);
return;
}
.............
}
部份效果图
点击下载代码
http://download.csdn.net/detail/lideguo1979/8369525
未完待续..................