当前位置:   article > 正文

【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总_cocos2d 回调函数

cocos2d 回调函数

一、按钮回调


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

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

2.宏定义bind方式创建回调.
  1. auto itemNor = Sprite::create("CloseNormal.png");
  2. auto menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
  3. auto menu = Menu::create(menuItem,nullptr);
  4. this->addChild(menu);
  5. void HelloWorld::menuCloseCallback(Ref* pSender)
  6. {
  7. log("show this msg.");
  8. }

3.MenuToggleItem回事件回调

  1. auto toggleSpNor = Label::createWithSystemFont("OPEN_BAME","WRYH",65);
  2. auto toggleSpSel = Label::createWithSystemFont("CLOSE_BAME","WRYH",65);
  3. auto toggleSpDis = Label::createWithSystemFont("DISABLE_BAME","WRYH",65);
  4. auto toggleItemNor = MenuItemLabel::create(toggleSpNor);
  5. auto toggleItemSel = MenuItemLabel::create(toggleSpSel);
  6. auto toggleItemDis = MenuItemLabel::create(toggleSpDis);
  7. auto toggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,this),toggleItemNor,toggleItemSel,nullptr);
  8. auto toggleMenu = Menu::create(toggleItem,nullptr);
  9. this->addChild(toggleMenu);
  10. void HelloWorld::toggleCallBack()
  11. {
  12. log("Do something when toggle did touched..");
  13. }



二、定时器回调

  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. void HelloWorld::gameStep(float dt)
  8. {
  9. log("on timer...");
  10. }

三、触屏事件回调

  1. auto touchEvt = cocos2d::EventListenerTouchOneByOne::create();
  2. touchEvt->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
  3. touchEvt->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
  4. touchEvt->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
  5. touchEvt->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);
  6. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvt,this);
  7. bool HelloWorld::onTouchBegan(cocos2d::Touch* touch,cocos2d::Event* evt)
  8. {
  9. log("Touch began..");
  10. return true;
  11. }
  12. void HelloWorld::onTouchMoved(cocos2d::Touch* touch,cocos2d::Event* evt)
  13. {
  14. log("Touch moved..");
  15. }
  16. void HelloWorld::onTouchEnded(cocos2d::Touch* touch,cocos2d::Event* evt)
  17. {
  18. log("Touch leave..");
  19. Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
  20. }
  21. void HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt)
  22. {
  23. log("Something was happend , touch event is cut..");
  24. }

四、动作回调


  1. auto callBack = CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));
  2. this->runAction(callBack);
  3. void HelloWorld::actionCallBack()
  4. {
  5. log("Do something when action did finished..");
  6. }

五、自定义事件回调


  1. auto callBack = [](EventCustom* evt)
  2. {
  3. log("catch an custom event!!");
  4. };
  5. cocos2d::EventListenerCustom* customEvt = EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);
  6. //注册自定义事件(处理优先级为12)
  7. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12);
  8. //抛出自定义事件
  9. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/573764
推荐阅读
相关标签
  

闽ICP备14008679号