Cocos2d-x 3.x事件分发机制总结
- 触摸事件 : EventListenerTouchOneByOne、EventListenerTouchAllAtOnce
- 鼠标响应事件 : EventListenerMouse
- 键盘响应事件 : EventListenerKeyboard
- 加速计事件 : EventListenerAcceleration
- 自定义事件 : EventListenerCustom
- 物理碰撞事件 : EventListenerPhysicsContact
- 游戏手柄事件 : EventListenerController
// class EventDispatcher : public Ref {
/** * 添加监听器
* - addEventListenerWithSceneGraphPriority
* - addEventListenerWithFixedPriority
* - addCustomEventListener
*/
//使用 场景图的优先级 为指定事件添加一个监听.
//listener : 指定要监听的事件.
//node : 这个节点的绘制顺序是基于监听优先级.
//优先级 : 0
void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
//使用 一定的优先级 为指定事件添加一个监听.
//listener : 指定要监听的事件.
//fixedPriority : 这个监听器的固定优先级.
//优先级 : fixedPriority。(但是不能为0,因为他是场景图的基本优先级)
void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
//用户自定义监听器
EventListenerCustom* addCustomEventListener(const std::string &eventName, const std::function& callback);
/**
* 删除监听器
* - removeEventListener
* - removeEventListenersForType
* - removeEventListenersForTarget
* - removeCustomEventListeners
* - removeAllEventListeners
*/
//删除指定监听器
void removeEventListener(EventListener* listener);
//删除某类型对应的所有监听器
//EventListener::Type::
// 单点触摸 : TOUCH_ONE_BY_ONE
// 多点触摸 : TOUCH_ALL_AT_ONCE
// 键盘 : KEYBOARD
// 鼠标 : MOUSE
// 加速计 : ACCELERATION
// 自定义 : CUSTOM
void removeEventListenersForType(EventListener::Type listenerType);
//删除绑定在节点target上的所有监听器
void removeEventListenersForTarget(Node* target, bool recursive = false);
//删除名字为customEventName的所有自定义监听器
void removeCustomEventListeners(const std::string& customEventName);
//移除所有监听器 void removeAllEventListeners();
/**
* 暂停、恢复在节点target上的所有监听器
* - pauseEventListenersForTarget
* - resumeEventListenersForTarget
*/
void pauseEventListenersForTarget(Node* target, bool recursive = false);
void resumeEventListenersForTarget(Node* target, bool recursive = false);
/**
* 其他
* - setPriority
* - setEnabled
* - dispatchEvent
* - dispatchCustomEvent
*/
//设置某监听器的优先级
void setPriority(EventListener* listener, int fixedPriority);
//启用事件分发器
void setEnabled(bool isEnabled);
bool isEnabled() const;
//手动派发自定义事件
void dispatchEvent(Event* event);
//给名字为eventName的自定义监听器, 绑定用户数据
void dispatchCustomEvent(const std::string &eventName, void *optionalUserData = nullptr); } //
- 优先级越低,越先响应事件。
- 如果优先级相同,则上层的(z轴)先接收触摸事件。
//
static EventListenerTouchOneByOne* create();
std::function onTouchBegan; //只有这个返回值为 bool
std::function onTouchMoved;
std::function onTouchEnded;
std::function onTouchCancelled;
//
使用举例:
//
//获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//创建单点触摸监听器 EventListenerTouchOneByOne
auto touchListener = EventListenerTouchOneByOne::create();
//单点触摸响应事件绑定
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled, this);
//在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
//单点触摸事件响应函数
bool onTouchBegan(Touch *touch, Event *unused_event) { CCLOG("began"); return true; }
void onTouchMoved(Touch *touch, Event *unused_event) { CCLOG("moved"); }
void onTouchEnded(Touch *touch, Event *unused_event) { CCLOG("ended"); }
void onTouchCancelled(Touch *touch, Event *unused_event) { CCLOG("cancelled"); } //
//
static EventListenerTouchAllAtOnce* create();
std::function<void(const std::vector&, Event*)> onTouchesBegan;
std::function<void(const std::vector&, Event*)> onTouchesMoved;
std::function<void(const std::vector&, Event*)> onTouchesEnded;
std::function<void(const std::vector&, Event*)> onTouchesCancelled;
//
使用举例:
// //获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//创建多点触摸监听器 EventListenerTouchAllAtOnce
auto touchesListener = EventListenerTouchAllAtOnce::create();
//多点触摸响应事件绑定
touchesListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
touchesListener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
touchesListener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
touchesListener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled, this);
//在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
dispatcher->addEventListenerWithSceneGraphPriority(touchesListener, this);
//多点触摸事件响应函数
void onTouchesBegan(const std::vector& touches, Event *unused_event) { CCLOG("began"); }
void onTouchesMoved(const std::vector& touches, Event *unused_event) { CCLOG("moved"); }
void onTouchesEnded(const std::vector& touches, Event *unused_event) { CCLOG("ended"); }
void onTouchesCancelled(const std::vector&touches, Event *unused_event) { CCLOG("cancelled"); } //
//
static EventListenerMouse* create();
std::function onMouseDown; //按下鼠标, 单击鼠标
std::function onMouseUp; //松开鼠标, 按下的状态下松开
std::function onMouseMove; //移动鼠标, 在屏幕中移动
std::function onMouseScroll; //滚动鼠标, 滚动鼠标的滚轮
//
使用举例:
// //获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//创建鼠标事件监听器 EventListenerMouse
EventListenerMouse* mouseListenter = EventListenerMouse::create();
//鼠标事件响应函数
mouseListenter->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown, this);
mouseListenter->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp, this);
mouseListenter->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove, this);
mouseListenter->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll, this);
//添加鼠标事件监听器,事件响应处理委托给this
dispatcher->addEventListenerWithSceneGraphPriority(mouseListenter, this);
//事件响应函数
void onMouseDown(Event* event) { CCLOG("Down"); }
void onMouseUp(Event* event) { CCLOG("UP"); }
void onMouseMove(Event* event) { CCLOG("MOVE"); }
void onMouseScroll(Event* event) { CCLOG("Scroll"); }
//
//
static EventListenerKeyboard* create();
std::function onKeyPressed; //按下某键
std::function onKeyReleased; //松开某键
//键盘按键枚举类型 EventKeyboard::KeyCode
//KeyCode的值对应的不是键盘的键值、也不是ASCII码,只是纯粹的枚举类型
//如:
// EventKeyboard::KeyCode::KEY_A
// EventKeyboard::KeyCode::KEY_1
// EventKeyboard::KeyCode::KEY_F1
// EventKeyboard::KeyCode::KEY_SPACE
// EventKeyboard::KeyCode::KEY_ALT
// EventKeyboard::KeyCode::KEY_SHIFT
//
使用举例:
// //获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//创建键盘按键事件监听器
EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create();
//绑定事件响应函数
keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
//添加监听器
dispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
//事件响应函数
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
if (EventKeyboard::KeyCode::KEY_J == keyCode) {
CCLOG("Pressed: J");
}
}
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
if (EventKeyboard::KeyCode::KEY_SPACE == keyCode) {
CCLOG("Released: SPACE");
}
}
//
//加速计信息 class Acceleration { double x; double y; double z; }; //
Device::setAccelerometerEnabled(true);
//
static EventListenerAcceleration* create(const std::function& callback);
std::function onAccelerationEvent;
//
4、使用举例
// //标签: 显示加速计信息
label = Label::createWithTTF("no used", "Marker Felt.ttf", 12);
label->setPosition(visibleSize / 2);
this->addChild(label);
//小球: 可视化加速计
ball = Sprite::create("ball.png");
ball->setPosition(visibleSize / 2);
this->addChild(ball);
//获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//需要开启移动设备的加速计
Device::setAccelerometerEnabled(true);
//创建加速计事件监听器
auto accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAccelerationEvent, this));
//添加加速计监听器
dispatcher->addEventListenerWithSceneGraphPriority(accelerationListener, this);
//事件响应函数
void HelloWorld::onAccelerationEvent(Acceleration* acceleration, Event* event) {
char s[100];
sprintf(s, "X: %f; Y: %f; Z:%f; ", acceleration->x, acceleration->y, acceleration->z);
label->setString(s); //改变小球ball的位置
float x = ball->getPositionX() + acceleration->x * 10;
float y = ball->getPositionY() + acceleration->y * 10;
Vec2 pos = Vec2(x, y);
pos.clamp(ball->getContentSize() / 2, Vec2(288, 512) - ball->getContentSize() / 2);
ball->setPosition(pos); //设置位置
}
//
//
//eventName : 监听器名字
//callback : 监听器函数
static EventListenerCustom* create(const std::string& eventName, const std::function& callback);
//
//
EventCustom event("your_event_type");
dispatcher->dispatchEvent(&event);
//
3、使用举例
//
//获取事件分发器
auto dispatcher = Director::getInstance()->getEventDispatcher();
//创建自定义事件监听器
//监听器名字 : "custom_event"
//事件响应函数: HelloWorld::onCustomEvent
auto customListener = EventListenerCustom::create("custom_event", CC_CALLBACK_1(HelloWorld::onCustomEvent, this));
//添加自定义事件监听器,优先权为1
dispatcher->addEventListenerWithFixedPriority(customListener, 1);
//手动分发监听器的事件,通过dispatchEvent
EventCustom event = EventCustom("custom_event");
dispatcher->dispatchEvent(&event);
//
事件响应函数
void HelloWorld::onCustomEvent(EventCustom* event) {
CCLOG("onCustomEvent");
}
//
- 每个自定义的事件监听器,都有一个监听器名字eventName。
- 需要手动通过 dispatcher->dispatchEvent(&event); 来手动将事件分发出去。
- 可以通过 dispatcher->dispatchCustomEvent(,); 来给自定义事件监听器绑定一个用户数据。
Cocos2d-x 3.x事件分发机制总结的更多相关文章
- Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制
EventDispatcher事件分发机制先创建事件,注册到事件管理中心_eventDispatcher,通过发布事件得到响应进行回调,完成事件流. 有五种不同的事件机制:EventListenerT ...
- cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析
(一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...
- Cocos2d-X研究之v3.x 事件分发机制具体解释
事件分发机制 " src="http://www.cgzhw.com/wp-content/uploads/2014/07/inherent3.png" style=&q ...
- Android事件分发机制浅谈(一)
---恢复内容开始--- 一.是什么 我们首先要了解什么是事件分发,通俗的讲就是,当一个触摸事件发生的时候,从一个窗口到一个视图,再到一个视图,直至被消费的过程. 二.做什么 在深入学习android ...
- Android事件分发机制浅谈(二)--源码分析(ViewGroup篇)
上节我们大致了解了事件分发机制的内容,大概流程,这一节来分析下事件分发的源代码. 我们先来分析ViewGroup中dispatchTouchEvent()中的源码 public boolean dis ...
- Atitit View事件分发机制
1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...
- Android Touch事件分发机制学习
Android 事件分发机制 ViewGroup dispatchTouchEvent 返回true dispatchTouchEvent: Activity ACTION_DOWN Myrelat ...
- android 事件分发机制
1.View的事件分发机制 一个button,简单一点就是onTouch,还有onclick事件,我们一个一个来分析 首先响应的是dispatchTouchEvent public boolean d ...
- Android View 事件分发机制 源码解析 (上)
一直想写事件分发机制的文章,不管咋样,也得自己研究下事件分发的源码,写出心得~ 首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个My ...
- Android中View的事件分发机制
简介 事件也称MotionEvent,事件分发机制就是对MotionEvent事件的分发过程,即当一个MotionEvent发生之后,系统需要把这个事件传递给一个具体的View. 点击事件的分发过程由 ...
随机推荐
- 【转】Android 图层引导帮助界面制作
2012-11-02 10:31 1979人阅读 评论(0) 收藏 举报 原文:http://www.cnblogs.com/beenupper/archive/2012/07/18/2597504. ...
- 【转】 教你如何创建类似QQ的android弹出菜单
原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...
- Liunx readlink命令
readlink命令 分类: Shell 2013-07-13 16:41 417人阅读 评论(0) 收藏 举报 readlink是linux系统中一个常用工具,主要用来找出符号链接所指向的位置. 在 ...
- windows下grunt安装提示不成功
在电脑按了node.js之后,在cmd中 输入 npm install -g grunt-cli 注意啦 不是 npm install -g grunt-cli 中间多了一个空格就安装不成功了,这个空 ...
- IOS UI 第四篇:基本UI
ViewController 应用 再第一个XIB页面创建另一个XIB页面,并且通过按钮调用它 - (IBAction)GoSecond:(id)sender { secondVie ...
- [Usaco2008 Feb]Meteor Shower流星雨[BFS]
Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的 ...
- 通过Func 委托理解委托和匿名方法及Lambda 表达式
Func<T, TResult> 委托 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法. 命名空间: System 程序集: mscorlib(在 mscorlib.d ...
- 企业架构研究总结(39)——TOGAF架构能力框架之架构委员会和架构合规性
3. 架构委员会 正如前面所说,一个用来对架构治理策略的实现进行监督的跨组织的架构委员会是架构治理策略成功的主要要素之一.架构委员会应该能够代表所有主要干系人的需求,并且通常还需要对整个架构的审查及维 ...
- IOS7学习之路二(处理ios6到ios7后UITableView的两个显示问题)
1.在ios6开发的项目,当用ios7的虚拟机显示的时候会出现UINavigationItem遮挡TableView的问题: 下面是对比显示效果: 我的处理方法是: 在UITableViewContr ...
- 支付宝移动支付开发详细教程服务端采用.net mvc webapi(C#)
转自:http://www.kwstu.com/ArticleView/netmvc_201511132005431321 最近开发手机app需要实现移动支付功能,由于考虑支付安全将支付宝生成签名写到 ...