【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 ...
随机推荐
- cuda yv12_to_rgb24
前言 项目需要将yv12转rgb24,由于基于x86平台,开始就没多想,直接用ipp加速实现了,后来在评估项目瓶颈的时候发现,1080p的视频每一帧转换居然要花8ms,刚好项目里有用到nvidia g ...
- 【乱入】Uva11021麻球繁衍
就是根据概率公式入门算算. #include<bits/stdc++.h> ; int n,m,k; double p[N],f[N]; int main(){ int T;scanf(& ...
- 【bzoj2242】计算器
#include<bits/stdc++.h> #define inf 1000000000 using namespace std; typedef long long ll; ?a:g ...
- git常用命令收集
[git远程操作命令] 1.$ git remote –v #查看本地配置的所有远程仓库,内容为远程仓库的地址和本地别名 harvey@harvey:~/node$ git remote -v nod ...
- jq TAB切换
<a href="http://www.w3.org/1999/xhtml">" target="_blank">http://ww ...
- 《Java编程思想》笔记 第三章 操作符
1.操作符种类: 运算顺序1-7 一元操作符(单目操作符) - 负号, + 正号,--递减,++递增 算术操作符 + - * / % 移位操作符 <<左移(低位补0),>&g ...
- 记录一下安卓本地文件File处理的问题
在项目中更新软件下载更新包的时候删除系统文件夹会报错导致下载失败 研究了下是因为目录不存在导致的由于系统自带下载目录,但是一些极限操作当目录不存在时就需要处理下 项目中的解决方案 File f = n ...
- 用 grunt-contrib-connect 构建实时预览开发环境 实时刷新
本文基本是参照着 用Grunt与livereload构建实时预览的开发环境 实操了一遍,直接实现能实时预览文件列表,内容页面.不用刷新页面了,这比以前开发网页程序都简单. 这里要用到的 Grunt 插 ...
- 配置虚拟主机 和 打war包
配置一台虚拟主机? 在[tomcat]/conf/server.xml文件中的<Engine>标签内部添加一个<Host>标签: <H ...
- 求第N个回文数 模板
备忘. /*看到n可以取到2*10^9.说明普通方法一个个暴力计算肯定会超时的,那打表呢?打表我们要先写个打表的代码,这里不提供.打完表观察数据,我们会发现数据其实是有规律的.完全不需要暴力的把所有数 ...