CocoStudio 在c++響應回撥方法
阿新 • • 發佈:2019-02-16
cocos2dx 3.9
cocostudio 3.9
今天用到了按鈕的回撥方法
回撥方法有Click和Touch兩種。分別是對應cocos2dx的addClickEventListener和addTouchEventListener.
看程式碼
//依次對應csb檔案的按鈕名稱 std::string btnName[]={"singlegame_btn","mulgame_btn","music_btn","loadgame_btn","exitgame_btn"}; for(auto n:btnName) { auto btn = dynamic_cast<Widget*>(csb->getChildByName(n)); //獲取回撥函式名稱 auto callbackName = btn->getCallbackName(); //獲取回撥型別 auto callbackType = btn->getCallbackType(); //繫結到相應的函式 this->bindCallback(callbackName,callbackType,btn); }
然後我們看看bindCallback函式
在這裡面就是判斷當前所屬的事件,然後進行繫結void MenuScene::bindCallback(const std::string&callbackName,const std::string&callbackType,Widget*widget) { //單人遊戲按鈕 if(callbackName == "singleGame" && callbackType == "Click") widget->addClickEventListener(CC_CALLBACK_1(MenuScene::singleGame,this)); //多人遊戲按鈕 if(callbackName == "mulGame" && callbackType == "Click") widget->addClickEventListener(CC_CALLBACK_1(MenuScene::mulGame,this)); //音效操作按鈕 if(callbackName == "operateMusic" && callbackType == "Click") widget->addClickEventListener(CC_CALLBACK_1(MenuScene::operatorMusic,this)); //載入遊戲按鈕 if(callbackName == "loadGame" && callbackType == "Click") widget->addClickEventListener(CC_CALLBACK_1(MenuScene::loadGame,this)); //退出遊戲按鈕 if(callbackName == "exitGame" && callbackType == "Click") widget->addClickEventListener(CC_CALLBACK_1(MenuScene::gameExit,this)); }
還有一種就是直接綁定了,不獲取回調函式名
auto loadGameBtn = dynamic_cast<Widget*>(csb->getChildByName("loadgame_btn"));
//也可以是addTouchEventListener()
loadGameBtn->addClickEventListener(CC_CALLBACK_1(MenuScene::loadGane,this));
兩種方法,孰優孰劣,看情況
,最後上一張效果圖
參考文件
http://www.cocoachina.com/cms/wap.php?action=article&id=11625