Cocos2d-x 3.2 大富翁遊戲項目開發-第十部分實現人物輪流行走,cocos2d-x項目開發

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第十部分實現人物輪流行走,cocos2d-x項目開發

現在只能讓我方角色行走,對手並沒有行動,如何?輪流行走呢?

有2種方法,一種是採用刷幀控制器在update進行,另一種採用訊息機制的形式實現,我們盡量避免使用刷幀控制器。就採用第二種訊息機制吧


我們在GameBaseScene中定義變數players_vector [Player*] 來存放所有的角色;

在添加新對象時,給該對象設定屬性_isMyTurn為true

這樣所有對象添加完畢後players_vector [Player*]所有的isMyTurn都為true,表示可以調用go方法,進行行走。


思路如下:


在go按鈕點擊之後,我方角色先走,這時就不應該再響應點擊事件了,我們可以採取讓go 按鈕點擊後消失,走完後設定isMyTurn   = false,表示我方讓出行走權。

然後調用在RicherGameController定義的方法pickOnePlayerToGo(),遍曆players_vector,取出一個isMyTurn   == true的運行go方法

運行完後,isMyTurn設定為false;

然後繼續調用pickOnePlayerToGo(),運行完畢後isMyTurn=false;

假如共有3個角色,運行3次後,所有對象的isMyTurn==false;

這時我們要把isMyTurn再次都設定為true,並讓go按鈕顯示出來,進行下一次輪換行走。


大體流程如下:



1、當點擊go按鈕之後,發送一個訊息,代表按鈕要消失,當其他角色都執行完行走之後,再發送一個訊息,讓go按鈕顯示

2、在GameBaseScene中定製訊息接收,根據收到的訊息,來讓go按鈕消失或顯示

boolGameBaseScene::init(){…………..//添加訊息接收器addNotificationObserver();returntrue;}//該方法定義接收一個MSG_GO的訊息,該訊息處理函數為receivedMsgForGo()voidGameBaseScene::addNotificationObserver(){NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(GameBaseScene::receivedMsgForGo),MSG_GO,NULL);}

voidGameBaseScene::receivedMsgForGo(Object* data){//把go 菜單放到了menu容器中,以後可以從中取出所有子功能表進行單獨處理利於後期擴充intretMsgType =((String*)data)->intValue();Vector<Node*>vecMenuItem = getMenu()->getChildren();SizewinSize = Director::getInstance()->getWinSize();//當收到的資訊是1時,表示要顯示各個menu項,讓菜單從螢幕外移動到螢幕中來if(retMsgType ==1){for(auto it=vecMenuItem.begin();it!=vecMenuItem.end();it++){Node* node = dynamic_cast<Node*>(*it);MoveBy* moveBy = MoveBy::create(0.3,ccp(-(node->getContentSize().width)*2,0));//RotateBy* rotateBy =RotateBy::create(1,360,10);//Action* action =Spawn::create(moveBy,rotateBy,NULL);node->runAction(moveBy);}}else//當收到的資訊是0時,表示要隱藏各個menu項,讓菜單從螢幕移動到右邊螢幕外面{for(auto it=vecMenuItem.begin();it!=vecMenuItem.end();it++){Node* node = dynamic_cast<Node*>(*it);MoveBy* moveBy = MoveBy::create(0.3,ccp((node->getContentSize().width)*2,0));//RotateBy* rotateBy =RotateBy::create(1,360,10);//Action* action =Spawn::create(moveBy,rotateBy,NULL);node->runAction(moveBy);}}log("received go message is: %d",retMsgType);}

voidGameBaseScene::goButtonCallback(cocos2d::CCObject *pSender){……………………………….//在go點擊後,發送go按鈕消失資訊NotificationCenter::getInstance()->postNotification(MSG_GO,String::create("0"));player1->startGo(rowVector,colVector);log("go button clicked over");}


voidRicherGameController::pickOnePlayerToGo(){for(auto it=GameBaseScene::players_vector.begin();it!=GameBaseScene::players_vector.end();it++){RicherPlayer* richerPlayer = dynamic_cast<RicherPlayer*>(*it);if(richerPlayer->getIsMyTurn()){RouteNavigation::getInstance()->getPath(richerPlayer,3,GameBaseScene::canPassGrid,GameBaseScene::tiledRowsCount,GameBaseScene::tiledColsCount);richerPlayer->startGo(RouteNavigation::getInstance()->getPathRow_vector(),RouteNavigation::getInstance()->getPathCols_vector());return;}}for(auto it=GameBaseScene::players_vector.begin();it!=GameBaseScene::players_vector.end();it++){RicherPlayer* richerPlayer = dynamic_cast<RicherPlayer*>(*it);richerPlayer->setIsMyTurn(true);}//代碼運行到這裡,代表都走完了,發送一個顯示go按鈕的資訊NotificationCenter::getInstance()->postNotification(MSG_GO,String::create("1"));}

做完這些之後,還做了一下掃尾工作,把人物的資金和體力顯示出來,並讓人物擷取隨機步數,然後行走。簡單就不解釋了。


點擊下載代碼   http://download.csdn.net/detail/lideguo1979/8301181


未完待續.....................




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.