【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 ...
随机推荐
- kvm的vmcall
这几个接口的区别在于参数个数的不用,本质是一样的.挑个参数最多的看下: static inline long kvm_hypercall4(unsigned int nr, unsigned long ...
- python基础===正则表达式(转)
正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技能,正则表达式的在不同的语言中使用方式可能 ...
- suse更改root密码
http://blog.csdn.net/george188/article/details/5383013 在SUSE Linux环境下,进入单用户模式仍然需要密码,因此通过进单用户模式恢复root ...
- 【luogu2574】xor的艺术
一道无聊的线段树题,写着玩玩而已…… #include<bits/stdc++.h> #define N 1000010 #define lson (o<<1) #define ...
- centos 7 卸载自带的jdk
# 查看jdk安装信息 rpm -qa|grep java 卸载已安装的jdk: # yum -y remove java java-1.7.0-*
- shell里面比较大小
#!/bin/bashif [ $1 -gt $2 ]then echo "$1>$2"else echo "$2>$1"fi# 数字判断一些命令 ...
- eclipse出现build path 错误
右击本项目-build path-config build path-libraries-发现有选项是带错误符号,于是点击edit然后点击alternative jre选择安装了的jre就解决问题了
- 【转载】Linux中profile、bashrc、bash_profile之间的区别和联系
如果你想对所有的使用bash的用户修改某个配置并在以后打开的bash都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个bash即可生效.~/.bash_profile:每个用户都可使用该文 ...
- 吊销openvpn证书
#cd /tools/openvpn-2.0.9/easy-rsa/2.0/ #source vars 低版本的openssl需要注销以下几个配置 vim openssl.cnf #[ pkcs11_ ...
- Unable to set localhost. This prevents creation of a GUID
原因:tomcat无法解析hostname 解决方案:解决方案:在/etc/hosts文件中添加hostname解析