【Cocos2d-x 3.0 基礎系列一】 各類回呼函數寫法匯總

來源:互聯網
上載者:User

一、按鈕回調

1. Lambda 運算式,C++11 Lambda 賦予了Cocos2d-x 3.0建立回呼函數的靈活性。

auto itemNor=Sprite::create("CloseNormal.png");auto menuItem=MenuItemSprite::create(itemNor,nullptr,nullptr,[](Ref* sender){log("show this msg.");});auto menu=Menu::create(menuItem,nullptr);this->addChild(menu);

2.宏定義bind方式建立回調.
auto itemNor=Sprite::create("CloseNormal.png");auto menuItem=MenuItemSprite::create(itemNor,nullptr,nullptr,CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));auto menu=Menu::create(menuItem,nullptr);this->addChild(menu);void HelloWorld::menuCloseCallback(Ref* pSender){log("show this msg.");}

3.MenuToggleItem回事件回調
auto toggleSpNor=Label::createWithSystemFont("OPEN_BAME","WRYH",65);auto toggleSpSel=Label::createWithSystemFont("CLOSE_BAME","WRYH",65);auto toggleSpDis=Label::createWithSystemFont("DISABLE_BAME","WRYH",65);auto toggleItemNor=MenuItemLabel::create(toggleSpNor);auto toggleItemSel=MenuItemLabel::create(toggleSpSel);auto toggleItemDis=MenuItemLabel::create(toggleSpDis);auto toggleItem=MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,this),toggleItemNor,toggleItemSel,nullptr);auto toggleMenu=Menu::create(toggleItem,nullptr);this->addChild(toggleMenu);void HelloWorld::toggleCallBack(){log("Do something when toggle did touched..");}



二、定時器回調
/*周期定時調用*/this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));/*倒計是定時調用一次*/this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\/*周期定時調用update需重寫update方法*/this->scheduleUpdate();void HelloWorld::gameStep(float dt){log("on timer...");}
三、觸屏事件回調
auto touchEvt    =cocos2d::EventListenerTouchOneByOne::create();touchEvt->onTouchBegan    =CC_CALLBACK_2(HelloWorld::onTouchBegan,this);touchEvt->onTouchMoved    =CC_CALLBACK_2(HelloWorld::onTouchMoved,this);touchEvt->onTouchEnded      =CC_CALLBACK_2(HelloWorld::onTouchEnded,this);touchEvt->onTouchCancelled     =CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvt,this);bool HelloWorld::onTouchBegan(cocos2d::Touch* touch,cocos2d::Event* evt){log("Touch began..");return true;}void HelloWorld::onTouchMoved(cocos2d::Touch* touch,cocos2d::Event* evt){log("Touch moved..");}void HelloWorld::onTouchEnded(cocos2d::Touch* touch,cocos2d::Event* evt){log("Touch leave..");Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);}void HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt){log("Something was happend , touch event is cut..");}

四、動作回調
auto callBack=CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));this->runAction(callBack);void HelloWorld::actionCallBack(){log("Do something when action did finished..");}

五、自訂事件回調
auto callBack=[](EventCustom* evt){log("catch an custom event!!");};cocos2d::EventListenerCustom* customEvt=EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);//註冊自訂事件(處理優先順序為12)Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12);//拋出自訂事件Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.