如果轮流到该角色行走时,吐司提示住院还有几天,当住院天数到期,该角色才可以继续行走
新建了几个精灵类
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
未完待续..................
总结以上是内存溢出为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第二十部分 螃蟹挡路(code)全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第二十部分 螃蟹挡路(code)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)