一、按钮回调
1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性。
- autoitemNor=Sprite::create("CloseNormal.png");
- automenuItem=MenuItemSprite::create(itemNor,nullptr,[](Ref*sender)
- {
- log("showthismsg.");
- });
- automenu=Menu::create(menuItem,nullptr);
- this->addChild(menu);
2.宏定义bind方式创建回调.
this));
- automenu=Menu::create(menuItem,nullptr);
- this->addChild(menu);
-
- voidHelloWorld::menuCloseCallback(Ref*pSender)
- }
3.MenuToggleItem回事件回调
autotoggleSpNor=Label::createWithSystemFont("OPEN_BAME","WRYH",65);
- autotoggleSpSel=Label::createWithSystemFont("CLOSE_BAME",65);
- autotoggleSpDis=Label::createWithSystemFont("DISABLE_BAME",248)"> autotoggleItemNor=MenuItemLabel::create(toggleSpNor);
- autotoggleItemSel=MenuItemLabel::create(toggleSpSel);
- autotoggleItemDis=MenuItemLabel::create(toggleSpDis);
- autotoggleItem=MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,153); font-weight:bold; background-color:inherit">this),toggleItemNor,toggleItemSel,nullptr);
- autotoggleMenu=Menu::create(toggleItem,153); font-weight:bold; background-color:inherit">this->addChild(toggleMenu);
-
- voidHelloWorld::toggleCallBack()
- {
- log("Dosomethingwhentoggledidtouched..");
- }
二、定时器回调
-
- this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));
- /*倒计是定时调用一次*/
- this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);
- /*周期定时调用update需重写update方法*/
- this->scheduleUpdate();
-
- voidHelloWorld::gameStep(floatdt)
- {
- log("ontimer...");
- }
autotouchEvt=cocos2d::EventListenerTouchOneByOne::create();
- touchEvt->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,153); font-weight:bold; background-color:inherit">this);
- touchEvt->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,153); font-weight:bold; background-color:inherit">this);
- touchEvt->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> touchEvt->onTouchCancelled=CC_CALLBACK_2(HelloWorld::onTouchCancelled,248)">
- Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvt,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> boolHelloWorld::onTouchBegan(cocos2d::Touch*touch,cocos2d::Event*evt)
- {
- log("Touchbegan..");
- returntrue;
- }
- voidHelloWorld::onTouchMoved(cocos2d::Touch*touch,cocos2d::Event*evt)
- log("Touchmoved..");
- voidHelloWorld::onTouchEnded(cocos2d::Touch*touch,248)"> log("Touchleave..");
- Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget( }
- voidHelloWorld::onTouchCancelled(cocos2d::Touch*touch,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("Somethingwashappend,toucheventiscut..");
- }
四、动作回调
autocallBack=CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));
- this->runAction(callBack);
- voidHelloWorld::actionCallBack()
- log("Dosomethingwhenactiondidfinished..");
- }
五、自定义事件回调
autocallBack=[](EventCustom*evt)
- log("catchancustomevent!!");
- };
- cocos2d::EventListenerCustom*customEvt=EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);
-
- Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12);
- //抛出自定义事件
- Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|