【唠叨】

自从3.0引用了C++11标准后,回调函数采用的新的函数适配器:std::functionstd::bind

而曾经的回调函数menu_selector、callfunc_selector、cccontrol_selector等都已经被无情的抛弃了。

取而代之的则是一系列的CC_CALLBACK_*

【致谢】

http://blog.csdn.net/crayondeng/article/details/18767407

http://blog.csdn.net/star530/article/details/21245565


【std::bind】

0、std::bind

请参照上面两篇文章。

1、CC_CALLBACK_*

cocos2dx总共使用了4个std::bind的宏定义,其重点就在于使用了std::bind进行函数适配

std::placeholders::_1 :不定参数。不事先指定,而是在调用的时候传入。

##__VA_ARGS__         :可变参数列表。

//
// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
//

2、变更的回调函数

> 动作函数  :CallFunc/CallFuncN

callfunc_selector / callfuncN_selector / callfuncND_selector

> 菜单项回调menu_selector

> 触摸事件  onTouchBeganonTouchMovedonTouchEnded

2.1、动作函数CallFunc

可以直接使用CC_CALLBACK_0、CC_CALLBACK_1,也可以直接使用std::bind。

> CallFunc :使用CC_CALLBACK_0。不带任何不定参数。

> CallFuncN:使用CC_CALLBACK_1。需要默认传入不定参数 placeholders::_1,其值为:调用该动作的对象(如sprite->runAction(callfun),那么默认的一个不定参数 _1 为 sprite)。

//
/***    函数动作*       - CallFunc*     - CallFuncN*        - CallFuncND与CallFuncO已被遗弃,请使用CallFuncN替代*/
//2.x版本CallFunc::create    (this, callfunc_selector  (HelloWorld::callback0) );CCCallFuncN::create (this, callfuncN_selector (HelloWorld::callback1) );CCCallFuncND::create(this, callfuncND_selector(HelloWorld::callback2), (void *)10 );//回调函数void HelloWorld::callback0() { }                     //CCCallFunc回调函数void HelloWorld::callback1(CCNode* node) { }         //CCCallFuncN回调函数void HelloWorld::callback2(CCNode* node,void* a) { } //CCCallFuncND回调函数,参数必须为void*//3.x版本//使用 CC_CALLBACK_*CallFunc::create ( CC_CALLBACK_0(HelloWorld::callback0, this) );CallFuncN::create( CC_CALLBACK_1(HelloWorld::callback1, this) );CallFuncN::create( CC_CALLBACK_1(HelloWorld::callback2, this, 0.5));//使用 std::bind//其中sprite为执行动作的精灵CallFunc::create (std::bind(&HelloWorld::callback0, this ) );CallFuncN::create(std::bind(&HelloWorld::callback1, this, sprite);CallFuncN::create(std::bind(&HelloWorld::callback2, this, sprite, 0.5));//回调函数void HelloWorld::callback0() { }void HelloWorld::callback1(Node* node) { }void HelloWorld::callback2(Node* node, float a) { }   //可自定义参数类型float//

当然,如果你对于std::bind很熟悉的话,对于CallFunc、CallFuncN回调函数的绑定,也可以全部都使用std::bind。

如下所示:

////callback0CallFunc::create(std::bind(&HelloWorld::callback0, this));//callback1CallFunc::create (std::bind(&HelloWorld::callback1, this, sprite));CallFuncN::create(std::bind(&HelloWorld::callback1, this, std::placeholders::_1));//callback2CallFunc::create (std::bind(&HelloWorld::callback2, this, sprite, 0.5));CallFuncN::create(std::bind(&HelloWorld::callback2, this, std::placeholders::_1, 0.5));//回调函数void HelloWorld::callback0() { }void HelloWorld::callback1(Node* node) { }void HelloWorld::callback2(Node* node, float a) { } //可自定义参数类型float
//

2.2、菜单项回调menu_selector

使用CC_CALLBACK_1,也可以直接使用std::bind。

//
//2.x版本MenuItemImage::create("1.png", "2.png", this, menu_selector(HelloWorld::callback));//3.x版本//CC_CALLBACK_1MenuItemImage::create("1.png", "2.png", CC_CALLBACK_1(HelloWorld::callback1, this));//std::bindMenuItemImage::create("1.png", "2.png", std::bind(&HelloWorld::callback1, this, std::placeholders::_1));//回调函数void HelloWorld::callback(Node* sender) { }
//

2.3、触控事件回调

使用CC_CALLBACK_2

////创建一个事件监听器类型为 单点触摸auto touchLisner = EventListenerTouchOneByOne::create();//绑定事件touchLisner->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);touchLisner->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);touchLisner->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);//回调函数virtual bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event); virtual void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event); virtual void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event);
//

3、未变更的回调函数

3.1、定时器回调schedule_selector

依旧使用schedule_selector

////定时器schedule(schedule_selector(HelloWorld::update), 1.0/60.0);//回调函数void HelloWorld::update(float dt) { }
//

3.2、按钮事件回调cccontrol_selector

依旧使用cccontrol_selector

////按钮事件绑定button->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::callback), Control::EventType::TOUCH_DOWN);//回调函数void HelloWorld::callback(Node* sender, Control::EventType controlEvent) { }
//

4、扩展回调函数

在3.x版本中,事件的回调函数可以带任意个自定义的参数啦。

举个栗子:(以菜单项回调函数为例)

        请看回调函数callback4。

//auto sprite = Sprite::create("CloseNormal.png");sprite->setPosition(Vec2(visibleSize / 2) );this->addChild(sprite);auto itemImage = MenuItemImage::create("CloseNormal.png", "CloseNormal.png", std::bind(&HelloWorld::callback4, this, std::placeholders::_1, sprite, 10, 0.5));itemImage->setPosition(Vec2(visibleSize / 4));auto pMenu = Menu::create(itemImage, NULL);pMenu->setPosition(Vec2::ZERO);this->addChild(pMenu);//回调函数void HelloWorld::callback4(Node* sender, Sprite* bg, int a, float b){bg->setScale(a * b);}
//

转载于:https://blog.51cto.com/shahdza/1553051

cocos2dx[3.2](11)——新回调函数std::bind相关推荐

  1. C++11新特性之std::bind()

    文章转载自:http://www.jellythink.com/archives/773 Cocos2d-x中有如下代码: // new callbacks based on C++11#define ...

  2. C++11 新特性之std::thread

    C++11 新特性之std::thread 原文:https://blog.csdn.net/oyoung_2012/article/details/78958274 从C++11开始,C++标准库已 ...

  3. C++11新特性之std::function<>

    转自http://www.jellythink.com/archives/771 看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Eve ...

  4. C++11新特性之std::function std::bind

    std::function 在c++98&03时我们通常使用回调函数来处理一些需要某种场景下才触发的回调操作,但回调函数有一个限制就是它只能赋值给同类型的全局或者静态函数,对于其他有相同的返回 ...

  5. cocos2d-x游戏开发(十三)细说回调函数

    欢迎转载:http://blog.csdn.net/fylz1125/article/details/8546607 cocos2d-x中有大量的回调函数的应用,主要有以下几类,看下CCObject. ...

  6. C++11新特性以及std::thread多线程编程

    一 .C++11新特性 1. auto 类型推导 1.1 当=号右边的表达式是一个引用类型时,auto会把引用抛弃,直接推导出原始类型: 1.2 当=号右边的表达式带有const属性时,auto不会使 ...

  7. C++11新特性之std::advance函数

    将某个迭代器前进到指定的位置上,例如: // advance example #include <iostream> // std::cout #include <iterator& ...

  8. C++11新特性之 std::forward(完美转发)

    上篇博客对右值.右值引用都做了简要介绍. 我们也要时刻清醒,有时候右值会转为左值,左值会转为右值. (也许"转换"二字用的不是很准确) 如果我们要避免这种转换呢? 我们需要一种方法 ...

  9. C++11 std::function, std::bind, std::ref, std::cref

    C++11 std::function, std::bind, std::ref, std::cref 转自:http://www.jellythink.com/ std::function 看看这段 ...

最新文章

  1. html5自定义属性作用,html5自定义属性:如何获取自定义属性值(附代码)
  2. Flash ActionScript (23)三天学透as3.0之第二天
  3. 霍夫变换概述和标准霍夫变换
  4. 扒一扒我遇见过哪些厌恶的技术面试官
  5. (转)PowerHA完全手册(一,二,三)
  6. python程序中想使用正则表达式_python中正则表达式的使用方法
  7. centos 文件夹网络连接_CentOS的网络配置的命令详解
  8. 《UG NX10中文版完全自学手册》——2.4 布局
  9. GPS之Ublox方案设计
  10. dnf远古深渊出传说详解
  11. 样条曲线转换为NUBRS曲线
  12. gbox推荐源_分享一批自己用的软件源 gbox软件源
  13. nc 单据模板公式
  14. SWFUpload学习记录
  15. 5G+AIoT趋势下,智慧社区的发展机遇与趋势
  16. 阿里云个人申请短信验证码申请总是失败
  17. vue等单页面应用及其优缺点
  18. 看完这篇文章APP关键词覆盖增加70000|互联网行业公会
  19. weui 自定义datepicker 年月日 上午下午 四级联动的实现
  20. Auto.js制作蓝奏软件库app

热门文章

  1. SpringBoot实战教程(8)| 整合mybatis-plus
  2. 给网站加上“新年快乐”灯笼源码
  3. Java POI 读取Excel-从开始到实例
  4. Alibaba Druid 源码阅读(一) 数据库连接池初步
  5. polycom安卓手机客户端_Spark,安卓上最好用的邮件客户端来了!附App下载
  6. c语言sort函数排序二维数组,关于C++ 的 sort 对二维数组排序。该如何解决
  7. MATLAB中的线性插值
  8. vue.3.0 dom赋值_Vue 3.0 快速入门
  9. (转)tomcat配置访问项目时不需要加项目名称
  10. java h5 交互 传数组, JS数组/对象的值为什么变了?你需要深入理解对象的值传递...