【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总
一、button回调
1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性。
auto itemNor = Sprite::create("CloseNormal.png");
auto menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,[](Ref* sender)
{
log("show this msg.");
});
auto menu = Menu::create(menuItem,nullptr);
this->addChild(menu);
2.宏定义bind方式创建回调.
auto itemNor = Sprite::create("CloseNormal.png");
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.");
}
3.MenuToggleItem回事件回调
auto toggleSpNor = Label::createWithSystemFont("OPEN_BAME","WRYH",65);
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..");
}
二、定时器回调
/*周期定时调用*/
this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));
/*倒计是定时调用一次*/
this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\
/*周期定时调用update需重写update方法*/
this->scheduleUpdate(); void HelloWorld::gameStep(float dt)
{
log("on timer...");
}
三、触屏事件回调
auto touchEvt = cocos2d::EventListenerTouchOneByOne::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()->addEventListenerWithSceneGraphPriority(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()->removeEventListenersForTarget(this);
}
void HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt)
{
log("Something was happend , touch event is cut..");
}
四、动作回调
auto callBack = CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));
this->runAction(callBack); void HelloWorld::actionCallBack()
{
log("Do something when action did finished..");
}
五、自己定义事件回调
auto callBack = [](EventCustom* evt)
{
log("catch an custom event!!");
};
cocos2d::EventListenerCustom* customEvt = EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);
//注冊自己定义事件(处理优先级为12)
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12); //抛出自己定义事件
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");
【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总的更多相关文章
- Entity Framework 5.0基础系列
1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...
- mysql 开发基础系列18 存储过程和函数(下)
1. 光标的使用(游标) 在存储过程和函数中可以使用光标对结果集进行循环的处理,光标使用包括光标的声明,open ,fetch,close. 下面在存储过程中使用一个光标, 这个举例中光标里的逻辑不重 ...
- mysql 开发基础系列17 存储过程和函数(上)
一. 概述 存储过程和函数是事先经过编译并存储在数据库中的一段sql语句集合,可以简化应用开发人员的很多工作,减少数据在数据库与应用服务器之间的传输,提高数据处理效率是有好处的.存储过程和函数的区别在 ...
- VMware ESXI6.0服务器安装系列:U盘安装问题汇总之网卡驱动安装
本文转载至:http://blog.51cto.com/huanwenli/1749298 在给物理服务器安装ESXI的过程中经常会遇到网卡驱动问题,如果是买的是Dell.HP.IBM等厂商的服务器, ...
- SQL基础系列(3)-变量、函数、存储过程等
1. 变量 定义变量 DECLARE @a INT 赋值 PRINT @a ) --select 赋值 SELECT @name='zcx' PRINT @name SELECT @name=F ...
- SQL基础系列(2)-内置函数--转载w3school
1. 日期函数 Mssql: SELECT GETDATE() 返回当前日期和时间 SELECT DATEPART(yyyy,OrderDate) AS OrderYear, DATEPART( ...
- Node.js系列基础学习-----回调函数,异步
Node.js基础学习 Node.js回调函数 Node.js异步编程的直接体现就是回调,异步编程依托回调来实现,但不是异步.回调函数在完成任务后就会被调用,Node有很多的回调函数,其所有的API都 ...
- Vue2.0 基础入门
前言:" 今生遇汝,何其幸哉:于我蒙昧之时遇到你,于我大雾初透之时爱上你,于我大智初醒之时沉沦你. " 官网: 介绍 - Vue.js (vuejs.org) 指令与修饰符 创建实 ...
- cocos2D v3.x中动作回调函数的变化
cocos2D v3.x版本中的动作的回调函数不能再带任何参数并且不能返回任何值. 官方给出的传递参数的办法是: 选择器(selector)不能带有任何形参,选择器需要的参数必须通过ivar或prop ...
随机推荐
- shell浅谈之三for、while、until循环【转】
转自:http://blog.csdn.net/taiyang1987912/article/details/38929069 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[- ...
- Linux指令详解useradd groupadd passwd chpasswd chage 密码修改
Linux指令详解useradd groupadd passwd chpasswd chage 密码修改 http://speediness.blog.51cto.com/760841/1783661 ...
- canvas的用法
包括: 介绍. 基础入门.(兼容性.获取canvas上下文.绘制直线/描边,填充内容.绘制表格.) canvas是基于状态的绘图. 绘制矩形. 绘制圆形. 绘制文本. 绘制图片. 阴影. 渐变. 绘制 ...
- selenium3+python自动化50-环境搭建(firefox)【转载】
前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...
- poj 1654(利用叉积求面积)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17937 Accepted: 4957 Description ...
- jQuery验证控件jquery.validate.js使用说明+中文API(转)
一导入js库<script src="../js/jquery.js" type="text/javascript"></script> ...
- ubuntu、linux更换pip源
1.创建pip配置文件 vi ~/.pip/pip.conf 2.写入配置内容 [global] trusted-host = mirrors.aliyun.com index-url = http: ...
- Asp.net中web.config配置文件详解
Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...
- postman用法总结+newman持续集成
一.postman 1.GET 请求:点击Params,输入参数及value,可输入多个显示在URL链接上(GET请求的请求头与请求参数如在接口文档中无特别声明时可以不填) 2.POST请求:在bod ...
- 洛谷 P1577 切绳子【二分答案】
题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位. 输入输出格式 输入格式: 第一行两个整数N和K,接下来N行,描 ...