加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x 3.0 回调函数

发布时间:2020-12-14 20:07:59 所属栏目:百科 来源:网络整理
导读:一、按钮回调 1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性。 [cpp] view plain copy autoitemNor=Sprite::create( "CloseNormal.png" ); automenuItem=MenuItemSprite::create(itemNor,nullptr,[](Ref*sender) { log( "showthi

一、按钮回调

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

[cpp] view plain copy
  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. }

二、定时器回调

[cpp] view plain copy
  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");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读