在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐。

而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器EventDispatcher 来进行统一的管理。

事件监听器主要有:

触摸事件     EventListenerTouchOneByOneEventListenerTouchAllAtOnce

鼠标响应事件 EventListenerMouse

键盘响应事件 EventListenerKeyboard

加速计事件   EventListenerAcceleration

自定义事件   EventListenerCustom

物理碰撞事件 EventListenerPhysicsContact

游戏手柄事件 EventListenerController


【事件分发器】

事件分发器EventDispatcher,用于统一管理事件监听器的所有事件的分发。

1、_eventDispatcher

_eventDispatcher是Node的属性,通过Director::getInstance()->getEventDispatcher() 获得。

_eventDispatcher的工作由三部分组成:

(1)事件分发器 :EventDispatcher。

(2)事件类型   :EventTouch, EventKeyboard 等。

(3)事件监听器 :EventListenerTouch, EventListenerKeyboard 等。

监听器实现了各种触发后的逻辑,在适当时候由事件分发器分发事件类型,然后调用相应类型的监听器。

2、添加/删除监听器

添加监听器:addEventListenerWithSceneGraphPriority 

            addEventListenerWithFixedPriority 

删除监听器:removeEventListener 

            removeAllEventListeners 

3、主要函数

包含监听器的添加、删除、暂停、恢复,优先级的设置,手动分发事件等。

  1. //
  2. class EventDispatcher : public Ref
  3. {
  4. /**
  5. * 添加监听器
  6. * - addEventListenerWithSceneGraphPriority
  7. * - addEventListenerWithFixedPriority
  8. * - addCustomEventListener
  9. */
  10. //使用 场景图的优先级 为指定事件添加一个监听.
  11. //listener : 指定要监听的事件.
  12. //node : 这个节点的绘制顺序是基于监听优先级.
  13. //优先级 : 0
  14. void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
  15. //使用 一定的优先级 为指定事件添加一个监听.
  16. //listener : 指定要监听的事件.
  17. //fixedPriority : 这个监听器的固定优先级.
  18. //优先级 : fixedPriority。(但是不能为0,因为他是场景图的基本优先级)
  19. void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
  20. //用户自定义监听器
  21. EventListenerCustom* addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback);
  22. /**
  23. * 删除监听器
  24. * - removeEventListener
  25. * - removeEventListenersForType
  26. * - removeEventListenersForTarget
  27. * - removeCustomEventListeners
  28. * - removeAllEventListeners
  29. */
  30. //删除指定监听器
  31. void removeEventListener(EventListener* listener);
  32. //删除某类型对应的所有监听器
  33. //EventListener::Type::
  34. // 单点触摸 : TOUCH_ONE_BY_ONE
  35. // 多点触摸 : TOUCH_ALL_AT_ONCE
  36. // 键盘 : KEYBOARD
  37. // 鼠标 : MOUSE
  38. // 加速计 : ACCELERATION
  39. // 自定义 : CUSTOM
  40. void removeEventListenersForType(EventListener::Type listenerType);
  41. //删除绑定在节点target上的所有监听器
  42. void removeEventListenersForTarget(Node* target, bool recursive = false);
  43. //删除名字为customEventName的所有自定义监听器
  44. void removeCustomEventListeners(const std::string& customEventName);
  45. //移除所有监听器
  46. void removeAllEventListeners();
  47. /**
  48. * 暂停、恢复在节点target上的所有监听器
  49. * - pauseEventListenersForTarget
  50. * - resumeEventListenersForTarget
  51. */
  52. void pauseEventListenersForTarget(Node* target, bool recursive = false);
  53. void resumeEventListenersForTarget(Node* target, bool recursive = false);
  54. /**
  55. * 其他
  56. * - setPriority
  57. * - setEnabled
  58. * - dispatchEvent
  59. * - dispatchCustomEvent
  60. */
  61. //设置某监听器的优先级
  62. void setPriority(EventListener* listener, int fixedPriority);
  63. //启用事件分发器
  64. void setEnabled(bool isEnabled);
  65. bool isEnabled() const;
  66. //手动派发自定义事件
  67. void dispatchEvent(Event* event);
  68. //给名字为eventName的自定义监听器, 绑定用户数据
  69. void dispatchCustomEvent(const std::string &eventName, void *optionalUserData = nullptr);
  70. }
  71. //

4、关于事件监听器的优先权

通过 addEventListenerWithSceneGraphPriority 添加的监听器,优先权为0。

通过 addEventListenerWithFixedPriority 添加的监听器,可以自定义优先权,但不能为0。

优先级越低,越先响应事件。

如果优先级相同,则上层的(z轴)先接收触摸事件。

5、使用步骤

(1)获取事件分发器  :dispatcher = Director::getInstance()->getEventDispatcher();

(2)创建监听器      :auto listener = EventListenerTouchOneByOne::create();

(3)绑定响应事件函数:listener->onTouchBegan = CC_CALLBACK_2(callback, this);

(4)将监听器添加到事件分发器dispatcher中:

dispatcher->addEventListenerWithSceneGraphPriority(Listener, this);

(5)编写回调响应函数:

bool callback(Touch* touch, Event* event) { ... }


【触摸事件】

1、单点触摸:EventListenerTouchOneByOne

单点触摸监听器相关:

  1. //
  2. static EventListenerTouchOneByOne* create();
  3. std::function<bool(Touch*, Event*)> onTouchBegan; //只有这个返回值为 bool
  4. std::function<void(Touch*, Event*)> onTouchMoved;
  5. std::function<void(Touch*, Event*)> onTouchEnded;
  6. std::function<void(Touch*, Event*)> onTouchCancelled;
  7. //

使用举例:

  1. //
  2. //获取事件分发器
  3. auto dispatcher = Director::getInstance()->getEventDispatcher();
  4. //创建单点触摸监听器 EventListenerTouchOneByOne
  5. auto touchListener = EventListenerTouchOneByOne::create();
  6. //单点触摸响应事件绑定
  7. touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
  8. touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
  9. touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
  10. touchListener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled, this);
  11. //在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
  12. dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
  13. //单点触摸事件响应函数
  14. bool onTouchBegan(Touch *touch, Event *unused_event) { CCLOG("began"); return true; }
  15. void onTouchMoved(Touch *touch, Event *unused_event) { CCLOG("moved"); }
  16. void onTouchEnded(Touch *touch, Event *unused_event) { CCLOG("ended"); }
  17. void onTouchCancelled(Touch *touch, Event *unused_event) { CCLOG("cancelled"); }
  18. //

2、多点触摸:EventListenerTouchAllAtOnce

多点触摸监听器相关:

  1. //
  2. static EventListenerTouchAllAtOnce* create();
  3. std::function<void(const std::vector<Touch*>&, Event*)> onTouchesBegan;
  4. std::function<void(const std::vector<Touch*>&, Event*)> onTouchesMoved;
  5. std::function<void(const std::vector<Touch*>&, Event*)> onTouchesEnded;
  6. std::function<void(const std::vector<Touch*>&, Event*)> onTouchesCancelled;
  7. //

使用举例:

  1. //
  2. //获取事件分发器
  3. auto dispatcher = Director::getInstance()->getEventDispatcher();
  4. //创建多点触摸监听器 EventListenerTouchAllAtOnce
  5. auto touchesListener = EventListenerTouchAllAtOnce::create();
  6. //多点触摸响应事件绑定
  7. touchesListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
  8. touchesListener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
  9. touchesListener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
  10. touchesListener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled, this);
  11. //在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
  12. dispatcher->addEventListenerWithSceneGraphPriority(touchesListener, this);
  13. //多点触摸事件响应函数
  14. void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event) { CCLOG("began"); }
  15. void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event) { CCLOG("moved"); }
  16. void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event) { CCLOG("ended"); }
  17. void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event) { CCLOG("cancelled"); }
  18. //

【鼠标事件】

EventListenerMouse,主要用于监听鼠标的点击、松开、移动、滚轮的事件。

鼠标事件监听器相关:

  1. //
  2. static EventListenerMouse* create();
  3. std::function<void(Event* event)> onMouseDown; //按下鼠标, 单击鼠标
  4. std::function<void(Event* event)> onMouseUp; //松开鼠标, 按下的状态下松开
  5. std::function<void(Event* event)> onMouseMove; //移动鼠标, 在屏幕中移动
  6. std::function<void(Event* event)> onMouseScroll;//滚动鼠标, 滚动鼠标的滚轮
  7. //

使用举例:

  1. //
  2. //获取事件分发器
  3. auto dispatcher = Director::getInstance()->getEventDispatcher();
  4. //创建鼠标事件监听器 EventListenerMouse
  5. EventListenerMouse* mouseListenter = EventListenerMouse::create();
  6. //鼠标事件响应函数
  7. mouseListenter->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown, this);
  8. mouseListenter->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp, this);
  9. mouseListenter->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove, this);
  10. mouseListenter->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll, this);
  11. //添加鼠标事件监听器,事件响应处理委托给this
  12. dispatcher->addEventListenerWithSceneGraphPriority(mouseListenter, this);
  13. //事件响应函数
  14. void onMouseDown(Event* event) { CCLOG("Down"); }
  15. void onMouseUp(Event* event) { CCLOG("UP"); }
  16. void onMouseMove(Event* event) { CCLOG("MOVE"); }
  17. void onMouseScroll(Event* event) { CCLOG("Scroll"); }
  18. //

【键盘事件】

EventListenerKeyboard,主要用于监听键盘某个键的按下、松开的事件。

键盘事件监听器相关:

  1. //
  2. static EventListenerKeyboard* create();
  3. std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; //按下某键
  4. std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; //松开某键
  5. //键盘按键枚举类型 EventKeyboard::KeyCode
  6. //KeyCode的值对应的不是键盘的键值、也不是ASCII码,只是纯粹的枚举类型
  7. //如:
  8. // EventKeyboard::KeyCode::KEY_A
  9. // EventKeyboard::KeyCode::KEY_1
  10. // EventKeyboard::KeyCode::KEY_F1
  11. // EventKeyboard::KeyCode::KEY_SPACE
  12. // EventKeyboard::KeyCode::KEY_ALT
  13. // EventKeyboard::KeyCode::KEY_SHIFT
  14. //

使用举例:

  1. //
  2. //获取事件分发器
  3. auto dispatcher = Director::getInstance()->getEventDispatcher();
  4. //创建键盘按键事件监听器
  5. EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create();
  6. //绑定事件响应函数
  7. keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
  8. keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
  9. //添加监听器
  10. dispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
  11. //事件响应函数
  12. void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
  13. if (EventKeyboard::KeyCode::KEY_J == keyCode) {
  14. CCLOG("Pressed: J");
  15. }
  16. }
  17. void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
  18. if (EventKeyboard::KeyCode::KEY_SPACE == keyCode) {
  19. CCLOG("Released: SPACE");
  20. }
  21. }
  22. //

【加速计事件】

EventListenerAcceleration,主要用于监听移动设备的所受重力方向感应事件。

重力感应来自移动设备的加速计,通常支持 (X, Y, Z) 三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。

1、加速计信息类Acceleration

该类中每个方向的加速度,大小都为一个重力加速度大小。

  1. //加速计信息
  2. class Acceleration
  3. {
  4. double x; double y; double z;
  5. };
  6. //

2、开启加速计感应

在使用加速计事件监听器之前,需要先启用此硬件设备:

Device::setAccelerometerEnabled(true);

3、加速计监听器相关

  1. //
  2. static EventListenerAcceleration* create(const std::function<void(Acceleration*, Event*)>& callback);
  3. std::function<void(Acceleration*, Event*)> onAccelerationEvent;
  4. //

4、使用举例

  1. //
  2. //标签: 显示加速计信息
  3. label = Label::createWithTTF("no used", "Marker Felt.ttf", 12);
  4. label->setPosition(visibleSize / 2);
  5. this->addChild(label);
  6. //小球: 可视化加速计
  7. ball = Sprite::create("ball.png");
  8. ball->setPosition(visibleSize / 2);
  9. this->addChild(ball);
  10. //获取事件分发器
  11. auto dispatcher = Director::getInstance()->getEventDispatcher();
  12. //需要开启移动设备的加速计
  13. Device::setAccelerometerEnabled(true);
  14. //创建加速计事件监听器
  15. auto accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAccelerationEvent, this));
  16. //添加加速计监听器
  17. dispatcher->addEventListenerWithSceneGraphPriority(accelerationListener, this);
  18. //事件响应函数
  19. void HelloWorld::onAccelerationEvent(Acceleration* acceleration, Event* event)
  20. {
  21. char s[100];
  22. sprintf(s, "X: %f; Y: %f; Z:%f; ", acceleration->x, acceleration->y, acceleration->z);
  23. label->setString(s);
  24. //改变小球ball的位置
  25. float x = ball->getPositionX() + acceleration->x * 10;
  26. float y = ball->getPositionY() + acceleration->y * 10;
  27. Vec2 pos = Vec2(x, y);
  28. pos.clamp(ball->getContentSize() / 2, Vec2(288, 512) - ball->getContentSize() / 2);
  29. ball->setPosition(pos); //设置位置
  30. }
  31. //

5、实际效果

在电脑上看不出效果,需要移植到手机上,才能看到加速计的效果。


【自定义事件】

以上是系统自带的事件类型,事件由系统内部自动触发,如 触摸屏幕,键盘响应等。

EventListenerCustom 自定义事件,它不是由系统自动触发,而是人为的干涉

它的出现,使得2.x中的 观察者模式 NotificationCenter(订阅发布消息) 被无情的遗弃了。

在 3.x 中,使用EventListenerCustom来实现消息的订阅与发布。

学习它之前,最好了解一下 NotificationCenter 这个类的用法。

NotificationCenter 的用法参见:http://shahdza.blog.51cto.com/2410787/1611575

1、创建自定义监听器

该监听器,就相当于是订阅消息。即与NotificationCenter的 addObserver 类似。

  1. //
  2. //eventName : 监听器名字,即消息的名称
  3. //callback : 监听器函数,即消息的回调函数
  4. static EventListenerCustom* create(const std::string& eventName, const std::function<void(EventCustom*)>& callback);
  5. //

2、分发自定义事件

自定义的事件监听器,需要通过手动的方式,将事件分发出去。

> 通过 EventCustom(string eventName);       来设置需要发布消息的数据信息,eventName为消息名称。

其中EventCustom可以通过setUserData来绑定想要传递的消息数据。

> 通过 dispatcher->dispatchEvent(&event); 手动将事件分发出去。即发布消息。

这与NotificationCenter的 postNotification 类似。

  1. //
  2. EventCustom event("custom_event");
  3. event->setUserData((void*)123); // 绑定消息传递的数据,可以为任意类型void。
  4. dispatcher->dispatchEvent(&event); // 发布名称为"custom_event"的消息。
  5. //

3、使用举例

  1. //
  2. //获取事件分发器
  3. auto dispatcher = Director::getInstance()->getEventDispatcher();
  4. //创建自定义事件监听器
  5. //监听器名字 : "custom_event"
  6. //事件响应函数: HelloWorld::onCustomEvent
  7. auto customListener = EventListenerCustom::create("custom_event", CC_CALLBACK_1(HelloWorld::onCustomEvent, this));
  8. //添加自定义事件监听器,优先权为1
  9. dispatcher->addEventListenerWithFixedPriority(customListener, 1);
  10. //手动分发监听器的事件,通过dispatchEvent发布名称为custom_event的消息。
  11. EventCustom event = EventCustom("custom_event");
  12. event->setUserData((void*)123); // 绑定消息传递的数据,可以为任意类型void。
  13. dispatcher->dispatchEvent(&event);
  14. //消息事件回调函数
  15. void HelloWorld::onCustomEvent(EventCustom* event)
  16. {
  17. // 获取消息传递的数据
  18. int* data = (int*)event->getUserData()
  19. CCLOG("onCustomEvent data = %d", data);
  20. }
  21. //

4、说明

> 每个自定义的事件监听器,都有一个监听器名字eventName。即为订阅的消息名称。

> 需要通过 dispatcher->dispatchEvent(&event); 来手动将事件分发出去。即为发布消息。

> 可以通过 dispatcher->dispatchCustomEvent(,); 来给自定义事件监听器绑定一个用户数据。


【物理碰撞事件】

有待研究。。。

  1. //
  2. EventListenerPhysicsContact;
  3. EventListenerPhysicsContactWithBodies;
  4. EventListenerPhysicsContactWithGroup;
  5. EventListenerPhysicsContactWithShapes;
  6. //

【游戏手柄】

有待研究。。。

  1. //
  2. EventListenerController;
  3. //

cocos2dx[3.2](10) 新回调函数std::bind的更多相关文章

  1. cocos2dx[3.2](9) 新回调函数std::bind

    自从3.0引用了C++11标准后,回调函数采用的新的函数适配器:std::function.std::bind. 而曾经的回调函数menu_selector.callfunc_selector.ccc ...

  2. C++ 11新特性:std bind 原理简单图解(转载)

    本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现. bind 可以用来将用户提供的需要一个参数的函数转换成不需 ...

  3. cocos2dx 3.0 它 使用std::bind更换CC_CALLBACK_N

    在cocos2dx 3.0 版本号,回调函数本质4一个CC_CALLBACK_N 替换功能.N的回调函数的参数的数量的代表 1.让我们来看看这些CC_CALLBACK_N怎么用 比方action的回调 ...

  4. Android React Native组件的生命周期及回调函数

    熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...

  5. C语言笔记 08_函数指针&回调函数&字符串&结构体&位域

    函数指针 函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: / ...

  6. 初识python: 回调函数

    回调函数 简单理解就是:将一个函数通过参数的形式传递给另一个函数 #!/user/bin env python # author:Simple-Sir # time:2019/8/9 10:49 # ...

  7. [C/C++11]_[初级]_[std::bind介绍和使用]

    场景 1.C++11 引入了std::function 对象, 这个对象可以通过std::bind封装所有的函数, 并通过代理调用这个std::function的方式调用这个函数. 比如通过统一的方式 ...

  8. 【cocos2dx开发技巧10】cocosStudio的集成以及c++11的新特性

    转发.请保持地址:http://blog.csdn.net/stalendp/article/details/38880997 非常长时间没有碰cocos2dx了,近期又用起来了.花了好几个小时又一次 ...

  9. cocos2d-x 2.2.0 如何在lua中注册回调函数给C++

    cocos2d-x内部使用tolua进行lua绑定,但是引擎并没有提供一个通用的接口让我们可以把一个lua函数注册给C++层面的回调事件.翻看引擎的lua绑定代码,我们可以仿照引擎中的方法来做.值得吐 ...

随机推荐

  1. 43 java中的异常处理机制的简单原理和应用

  2. ELK——集中式日志系统

    https://www.ibm.com/developerworks/cn/opensource/os-cn-elk/index.html 基本流程是 Shipper 负责从各种数据源里采集数据,然后 ...

  3. Linux培训教程lgzip命令详解和使用实例

    gzip不仅可以用来压缩大的.较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式.据统计,gzip命令对文本文件有60%-70%的压缩率. 1.命令格式 ...

  4. TTTTTTTTTTTT CF 653D 送邮递员

    链接:给一张n个点m条带权边的有向图,有x个人从起点出发到终点,每个人带的都带相同重量的货物, 规定一条边最多能经过其上权的重量的货物,问最多能带多重的货物? 2 ≤ n ≤ 50, 1 ≤ m ≤  ...

  5. Springboot 使用JdbcTemplate

    Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer ...

  6. 【Vue】input textarea自动滚动到输入处

    由于我这里要把接口返回的日志不断地新增到textarea里,想实现自动滚动日志的效果. 1.首先定一个textarea类型的input组件 <el-input id="textarea ...

  7. sh_01_列表基本使用

    sh_01_列表基本使用 name_list = ["zhangsan", "lisi", "wangwu"] # 1. 取值和取索引 # ...

  8. 1003: [ZJOI2006]物流运输

    就我一开始写状压的吗? 调不过 后来发现(直接搜索)直接最短路就行了-- \(f[i]\)表示前\(i\)天最少需要多少 \(f[i] = min(f[j] + dis(j + 1, i))\) 然后 ...

  9. HDU2082 找单词

    问题分析 不难想到用母函数做. 令自变量\(x\)的次数就是单词价值,那么答案就是\(x\)的\(1\)次到\(50\)次的系数之和.由于我们只需要处理前\(51\)项,所以暴力多项式相乘即可. 举个 ...

  10. Codeforces 798D Mike and distribution (构造)

    题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...