[Cocos2d-x 3.0 basic Series A] all kinds of callback function writing Summary

Source: Internet
Author: User

I. Button callback

1. Lambda expressions, C ++ 11 Lambda gives Cocos2d-x 3.0 the flexibility to create callback functions.

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. Macro-defined bind method creation callback.
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 callback
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..");}



Ii. Timer callback
/* Periodical scheduled call */this-> schedule (SEL_SCHEDULE (& HelloWorld: gameStep )); /* indicates the scheduled call of scheduleOnce (SEL_SCHEDULE (& HelloWorld: gameStep), 3.0f ); \/* The update method must be rewritten to call update periodically */this-> scheduleUpdate (); void HelloWorld: gameStep (float dt) {log ("on timer... ");}
3. Touch Screen Event Callback
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..");}

Iv. Action callback
auto callBack=CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));this->runAction(callBack);void HelloWorld::actionCallBack(){log("Do something when action did finished..");}

5. Custom Event Callback
Auto callBack = [] (EventCustom * evt) {log ("catch an custom event !! ") ;}; Cocos2d: EventListenerCustom * customEvt = EventListenerCustom: create (" ME_CUSTOM_EVENT_TEST ", callBack); // register a Custom Event (with a processing priority of 12) Director :: getInstance ()-> getEventDispatcher ()-> addEventListenerWithFixedPriority (customEvt, 12); // throw a Custom Event Director: getInstance ()-> getEventDispatcher () -> dispatchCustomEvent ("ME_CUSTOM_EVENT_TEST ");





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.