Cocos2dx3.0 回调函数写法

来源:互联网 发布:按键精灵抓取网页数据 编辑:程序博客网 时间:2024/06/05 04:42

转自 ---http://blog.sina.com.cn/s/blog_6d193c030101h40e.html

一、按钮回调


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

[cpp]
  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方式创建回调.
[cpp] 
  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.   
  6. void HelloWorld::menuCloseCallback(Ref* pSender)  
  7.  
  8.     log("show this msg.");  
  9.  

3.MenuToggleItem回事件回调

[cpp] 
  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.   
  8. auto toggleItem       MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,this),toggleItemNor,toggleItemSel,nullptr);  
  9.   
  10. auto toggleMenu   Menu::create(toggleItem,nullptr);  
  11. this->addChild(toggleMenu);  
  12.   
  13. void HelloWorld::toggleCallBack()  
  14.  
  15.     log("Do something when toggle did touched..");  
  16.  



二、定时器回调

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1.   
  2. this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));  
  3.   
  4. this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\  
  5.   
  6. this->scheduleUpdate();  
  7.   
  8. void HelloWorld::gameStep(float dt)  
  9.  
  10.     log("on timer...");  
  11.  

三、触屏事件回调

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  7. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvt,this);  
  8.   
  9. bool HelloWorld::onTouchBegan(cocos2d::Touch* touch,cocos2d::Event* evt)  
  10.  
  11.     log("Touch began..");  
  12.     return true 
  13.  
  14. void HelloWorld::onTouchMoved(cocos2d::Touch* touch,cocos2d::Event* evt)  
  15.  
  16.     log("Touch moved..");  
  17.  
  18. void HelloWorld::onTouchEnded(cocos2d::Touch* touch,cocos2d::Event* evt)  
  19.  
  20.     log("Touch leave..");  
  21.     Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);  
  22.  
  23. void HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt)  
  24.  
  25.     log("Something was happend touch event is cut..");  
  26.  

四、动作回调


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. auto callBack         CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));  
  2. this->runAction(callBack);  
  3.   
  4. void HelloWorld::actionCallBack()  
  5.  
  6.     log("Do something when action did finished..");  
  7.  

五、自定义事件回调


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. //抛出自定义事件  
  10. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");  


0 0
原创粉丝点击