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]
- auto
itemNor "CloseNormal.png");= Sprite::create( - auto
menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,[](Ref* sender) - {
-
log("show this );msg." - });
- auto
menu = Menu::create(menuItem,nullptr); - this->addChild(menu);
2.宏定义bind方式创建回调.
[cpp]
- auto
itemNor "CloseNormal.png");= Sprite::create( - auto
menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); - auto
menu = Menu::create(menuItem,nullptr); - this->addChild(menu);
-
- void
HelloWorld::menuCloseCallback(Ref* pSender) - {
-
log("show this );msg." - }
[cpp]
- auto
toggleSpNor "OPEN_BAME","WRYH",65);= Label::createWithSystemFont( - auto
toggleSpSel = Label::createWithSystemFont("CLOSE_BAME","WRYH",65); - auto
toggleSpDis = Label::createWithSystemFont("DISABLE_BAME","WRYH",65); - auto
toggleItemNor = MenuItemLabel::create(toggleSpNor); - auto
toggleItemSel = MenuItemLabel::create(toggleSpSel); - auto
toggleItemDis = MenuItemLabel::create(toggleSpDis); -
- auto
toggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,this),toggleItemNor,toggleItemSel,nullptr); -
- auto
toggleMenu = Menu::create(toggleItem,nullptr); - this->addChild(toggleMenu);
-
- void
HelloWorld::toggleCallBack() - {
-
log("Do something );when toggle did touched.." - }
二、定时器回调
[cpp] view plaincopy
-
- this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));
-
- this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\
-
- this->scheduleUpdate();
-
- void
HelloWorld::gameStep( floatdt) - {
-
log("on timer..." ); - }
三、触屏事件回调
[cpp] view plaincopy
- auto
touchEvt = cocos2d::EventListenerTouchOneByO ne::create(); - touchEvt->onTouchBegan
= CC_CALLBACK_2(HelloWorld::onTouchBegan,this); - touchEvt->onTouchMoved
= CC_CALLBACK_2(HelloWorld::onTouchMoved,this); - touchEvt->onTouchEnded
= CC_CALLBACK_2(HelloWorld::onTouchEnded,this); - touchEvt->onTouchCancelled
= CC_CALLBACK_2(HelloWorld::onTouchCancelled,this); -
- Director::getInstance()->getEventDispatcher()->addEventListenerWithScen
eGraphPriority(touchEvt,this); -
- bool
HelloWorld::onTouchBegan(cocos2d::Touch* touch,cocos2d::Event* evt) - {
-
log("Touch began.." ); -
return true; - }
- void
HelloWorld::onTouchMoved(cocos2d::Touch* touch,cocos2d::Event* evt) - {
-
log("Touch moved.." ); - }
- void
HelloWorld::onTouchEnded(cocos2d::Touch* touch,cocos2d::Event* evt) - {
-
log("Touch leave.." ); -
Director::getInstance()->getEventDispatcher()->removeEventListenersForT arget(this); - }
- void
HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt) - {
-
log("Something was );happend , touch event is cut.." - }
四、动作回调
[cpp] view plaincopy
- auto
callBack this));= CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack, - this->runAction(callBack);
-
- void
HelloWorld::actionCallBack() - {
-
log("Do something );when action did finished.." - }
五、自定义事件回调
[cpp] view plaincopy
- auto
callBack = [](EventCustom* evt) -
{ -
log("catch an );custom event!!" -
}; - cocos2d::EventListenerCustom*
customEvt = EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack); - //注册自定义事件(处理优先级为12)
- Director::getInstance()->getEventDispatcher()->addEventListenerWithFixe
dPriority(customEvt,12); -
- //抛出自定义事件
- Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");
0 0
- Cocos2dx3.0 回调函数写法
- js 回调函数写法
- cocos2dx3.0自定义回调函数,传递自己想要的参数
- Flex 中回调函数的写法
- js回调函数的写法
- java 回调函数的写法
- JS中回调函数的写法
- 各种事件的回调函数写法
- js 回调函数的写法
- cocos绑定回调函数的写法
- dwr中回调函数带参数写法
- android写回调函数的写法和调用
- C语言回调函数的定义和写法
- cocos2dx3.2 谈谈精灵也能加入触摸事件回调函数简单使用,呢吗有木有更有的方法
- 【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总
- matlab gui(二)对象(axes,line[ezplot,plot],回调函数的写法)
- 静态页面参数传递&回调函数写法&快速排序的实现方法
- .NET 回调函数
- 如何教计算机认识手写数字(上)
- JVM-内存模型
- STK Component 矢量几何工具系列--坐标轴(Axes)转换基础
- Java 泛型
- oj第十二周训练——汉诺塔
- Cocos2dx3.0 回调函数写法
- 98. Validate Binary Search Tree
- 今天就要开始学习linux建站了,好好加油!
- 开发中常用英文单词
- 北京赛车游戏杀号技巧
- 解决JSP中的各种乱码问题
- UserLogin 存储过程
- MVVM + ReactiveCocoa 一些简单功能的实现
- 洛谷 p1036 选数 非搜索版本