Cocos2d-x 3.0 回调函数

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.0 回调函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@一、按钮回调

1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性。

  1. autoitemNor=Sprite::create("CloseNormal.png");
  2. automenuItem=MenuItemSprite::create(itemNor,nullptr,[](Ref*sender)
  3. {
  4. log("showthismsg.");
  5. });
  6. automenu=Menu::create(menuItem,nullptr);
  7. this->addChild(menu);
2.宏定义bind方式创建回调.
    this));
  1. automenu=Menu::create(menuItem,nullptr);
  2. this->addChild(menu);
  3. voidHelloWorld::menuCloseCallback(Ref*pSender)
  4. }
3.MenuToggleItem回事件回调
    autotoggleSpNor=Label::createWithSystemFont("OPEN_BAME","WRYH",65);
  1. autotoggleSpSel=Label::createWithSystemFont("CLOSE_BAME",65);
  2. autotoggleSpDis=Label::createWithSystemFont("DISABLE_BAME",248)"> autotoggleItemNor=MenuItemLabel::create(toggleSpNor);
  3. autotoggleItemSel=MenuItemLabel::create(toggleSpSel);
  4. autotoggleItemDis=MenuItemLabel::create(toggleSpDis);
  5. autotoggleItem=MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,153); font-weight:bold; background-color:inherit">this),toggleItemNor,toggleItemSel,nullptr);
  6. autotoggleMenu=Menu::create(toggleItem,153); font-weight:bold; background-color:inherit">this->addChild(toggleMenu);
  7. voidHelloWorld::toggleCallBack()
  8. {
  9. log("Dosomethingwhentoggledidtouched..");
  10. }

二、定时器回调

  1. /*周期定时调用*/
  2. this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));
  3. /*倒计是定时调用一次*/
  4. this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\
  5. /*周期定时调用update需重写update方法*/
  6. this->scheduleUpdate();
  7. voidHelloWorld::gameStep(floatdt)
  8. {
  9. log("ontimer...");
  10. }

三、触屏事件回调

    autotouchEvt=cocos2d::EventListenerTouchOneByOne::create();
  1. touchEvt->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,153); font-weight:bold; background-color:inherit">this);
  2. touchEvt->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,153); font-weight:bold; background-color:inherit">this);
  3. 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)">
  4. 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)
  5. {
  6. log("Touchbegan..");
  7. returntrue;
  8. }
  9. voidHelloWorld::onTouchMoved(cocos2d::Touch*touch,cocos2d::Event*evt)
  10. log("Touchmoved..");
  11. voidHelloWorld::onTouchEnded(cocos2d::Touch*touch,248)"> log("Touchleave..");
  12. Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget( }
  13. 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..");
  14. }

四、动作回调

    autocallBack=CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));
  1. this->runAction(callBack);
  2. voidHelloWorld::actionCallBack()
  3. log("Dosomethingwhenactiondidfinished..");
  4. }

五、自定义事件回调

    autocallBack=[](EventCustom*evt)
  1. log("catchancustomevent!!");
  2. };
  3. cocos2d::EventListenerCustom*customEvt=EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);
  4. //注册自定义事件(处理优先级为12)
  5. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12);
  6. //抛出自定义事件
  7. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");
原文链接:https://www.f2er.com/cocos2dx/344827.html

猜你在找的Cocos2d-x相关文章