Cocos2d-x v3.2笔记——Cocos2d-x 3.x新事件分发机制总结

前端之家收集整理的这篇文章主要介绍了Cocos2d-x v3.2笔记——Cocos2d-x 3.x新事件分发机制总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐。而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器EventDispatcher 来进行统一的管理。
@H_502_4@
事件监听器主要有:
  • 触摸事件 : EventListenerTouchOneByOne、EventListenerTouchAllAtOnce
  • 鼠标响应事件 : EventListenerMouse
  • 键盘响应事件 : EventListenerKeyboard
  • 加速计事件 : EventListenerAcceleration
  • 自定义事件 : EventListenerCustom
  • 物理碰撞事件 : EventListenerPhysicsContact
  • 游戏手柄事件 : EventListenerController
    @H_502_4@
@H_502_4@
【事件分发器】
事件分发器EventDispatcher,用于统一管理事件监听器的所有事件的分发。
1、_eventDispatcher
_eventDispatcher是Node的属性,通过Director::getInstance()->getEventDispatcher() 获得。
_eventDispatcher的工作由三部分组成:
(1)事件分发器 :EventDispatcher。
(2)事件类型 :EventTouch,EventKeyboard 等。
(3)事件监听器 :EventListenerTouch,EventListenerKeyboard 等。
监听器实现了各种触发后的逻辑,在适当时候由事件分发器分发事件类型,然后调用相应类型的监听器。
2、添加/删除监听器
添加监听器:addEventListenerWithSceneGraPHPriority,addEventListenerWithFixedPriority。
删除监听器:removeEventListener,removeAllEventListeners。
3、主要函数
包含监听器的添加删除、暂停、恢复,优先级的设置,手动分发事件等。
@H_502_4@
// @H_502_4@ class EventDispatcher : public Ref @H_502_4@ { @H_502_4@ /** @H_502_4@ * 添加监听器 @H_502_4@ * - addEventListenerWithSceneGraPHPriority @H_502_4@ * - addEventListenerWithFixedPriority @H_502_4@ * - addCustomEventListener @H_502_4@ */ @H_502_4@ //使用 场景图的优先级 为指定事件添加一个监听. @H_502_4@ //listener : 指定要监听的事件. @H_502_4@ //node : 这个节点的绘制顺序是基于监听优先级. @H_502_4@ //优先级 : 0 @H_502_4@ void addEventListenerWithSceneGraPHPriority(EventListener* listener,Node* node); @H_502_4@ @H_502_4@ //使用 一定的优先级 为指定事件添加一个监听. @H_502_4@ //listener : 指定要监听的事件. @H_502_4@ //fixedPriority : 这个监听器的固定优先级. @H_502_4@ //优先级 : fixedPriority。(但是不能为0,因为他是场景图的基本优先级) @H_502_4@ void addEventListenerWithFixedPriority(EventListener* listener,int fixedPriority); @H_502_4@ @H_502_4@ //用户自定义监听器 @H_502_4@ EventListenerCustom* addCustomEventListener(const std::string &eventName,const std::function& callback); @H_502_4@ @H_502_4@ @H_502_4@ /** @H_502_4@ * 删除监听器 @H_502_4@ * - removeEventListener @H_502_4@ * - removeEventListenersForType @H_502_4@ * - removeEventListenersForTarget @H_502_4@ * - removeCustomEventListeners @H_502_4@ * - removeAllEventListeners @H_502_4@ */ @H_502_4@ //删除指定监听器 @H_502_4@ void removeEventListener(EventListener* listener); @H_502_4@ @H_502_4@ //删除某类型对应的所有监听器 @H_502_4@ //EventListener::Type:: @H_502_4@ //单点触摸 : TOUCH_ONE_BY_ONE @H_502_4@ //多点触摸 : TOUCH_ALL_AT_ONCE @H_502_4@ //键盘 : KEYBOARD @H_502_4@ //鼠标 : MOUSE @H_502_4@ //加速计 : ACCELERATION @H_502_4@ //自定义 : CUSTOM @H_502_4@ void removeEventListenersForType(EventListener::Type listenerType); @H_502_4@ @H_502_4@ //删除绑定在节点target上的所有监听器 @H_502_4@ void removeEventListenersForTarget(Node* target,bool recursive = false); @H_502_4@ @H_502_4@ //删除名字为customEventName的所有自定义监听器 @H_502_4@ void removeCustomEventListeners(const std::string& customEventName); @H_502_4@ @H_502_4@ //移除所有监听器 @H_502_4@ void removeAllEventListeners(); @H_502_4@ @H_502_4@ @H_502_4@ /** @H_502_4@ * 暂停、恢复在节点target上的所有监听器 @H_502_4@ * - pauseEventListenersForTarget @H_502_4@ * - resumeEventListenersForTarget @H_502_4@ */ @H_502_4@ void pauseEventListenersForTarget(Node* target,bool recursive = false); @H_502_4@ void resumeEventListenersForTarget(Node* target,bool recursive = false); @H_502_4@ @H_502_4@ @H_502_4@ /** @H_502_4@ * 其他 @H_502_4@ * - setPriority @H_502_4@ * - setEnabled @H_502_4@ * - dispatchEvent @H_502_4@ * - dispatchCustomEvent @H_502_4@ */ @H_502_4@ //设置某监听器的优先级 @H_502_4@ void setPriority(EventListener* listener,int fixedPriority); @H_502_4@ @H_502_4@ //启用事件分发器 @H_502_4@ void setEnabled(bool isEnabled); @H_502_4@ bool isEnabled() const; @H_502_4@ @H_502_4@ //手动派发自定义事件 @H_502_4@ void dispatchEvent(Event* event); @H_502_4@ @H_502_4@ //给名字为eventName的自定义监听器,绑定用户数据 @H_502_4@ void dispatchCustomEvent(const std::string &eventName,void *optionalUserData = nullptr); @H_502_4@ } @H_502_4@ //
@H_502_4@
4、关于事件监听器的优先权@H_502_4@
通过 addEventListenerWithSceneGraPHPriority 添加的监听器,优先权为0。
通过 addEventListenerWithFixedPriority 添加的监听器,可以自定义优先权,但不能为0。
  • 优先级越低,越先响应事件。
  • 如果优先级相同,则上层的(z轴)先接收触摸事件。
    @H_502_4@
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) { ... }
@H_502_4@
【触摸事件】
1、单点触摸:EventListenerTouchOneByOne
单点触摸监听器相关:
// @H_502_4@ static EventListenerTouchOneByOne* create(); @H_502_4@ @H_502_4@ std::function onTouchBegan; //只有这个返回值为 bool @H_502_4@ std::function onTouchMoved; @H_502_4@ std::function onTouchEnded; @H_502_4@ std::function onTouchCancelled; @H_502_4@ //
@H_502_4@ 使用举例: @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@
// @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //创建单点触摸监听器 EventListenerTouchOneByOne @H_502_4@ auto touchListener = EventListenerTouchOneByOne::create(); @H_502_4@ @H_502_4@ //单点触摸响应事件绑定 @H_502_4@ touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this); @H_502_4@ touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this); @H_502_4@ touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this); @H_502_4@ touchListener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled,this); @H_502_4@ @H_502_4@ //在事件分发器中,添加触摸监听器,事件响应委托给 this 处理 @H_502_4@ dispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this); @H_502_4@ @H_502_4@ @H_502_4@ //单点触摸事件响应函数 @H_502_4@ bool onTouchBegan(Touch *touch,Event *unused_event) { CCLOG("began"); return true; } @H_502_4@ void onTouchMoved(Touch *touch,Event *unused_event) { CCLOG("moved"); } @H_502_4@ void onTouchEnded(Touch *touch,Event *unused_event) { CCLOG("ended"); } @H_502_4@ void onTouchCancelled(Touch *touch,Event *unused_event) { CCLOG("cancelled"); } @H_502_4@ //
@H_502_4@
2、多点触摸:EventListenerTouchAllAtOnce@H_502_4@
多点触摸监听器相关:
// @H_502_4@ static EventListenerTouchAllAtOnce* create(); @H_502_4@ @H_502_4@ std::function<void(const std::vector&,Event*)> onTouchesBegan; @H_502_4@ std::function<void(const std::vector&,Event*)> onTouchesMoved; @H_502_4@ std::function<void(const std::vector&,Event*)> onTouchesEnded; @H_502_4@ std::function<void(const std::vector&,Event*)> onTouchesCancelled; @H_502_4@ //
@H_502_4@ 使用举例: @H_502_4@
// @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //创建多点触摸监听器 EventListenerTouchAllAtOnce @H_502_4@ auto touchesListener = EventListenerTouchAllAtOnce::create(); @H_502_4@ @H_502_4@ //多点触摸响应事件绑定 @H_502_4@ touchesListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan,this); @H_502_4@ touchesListener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved,this); @H_502_4@ touchesListener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded,this); @H_502_4@ touchesListener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled,this); @H_502_4@ @H_502_4@ //在事件分发器中,添加触摸监听器,事件响应委托给 this 处理 @H_502_4@ dispatcher->addEventListenerWithSceneGraPHPriority(touchesListener,this); @H_502_4@ @H_502_4@ @H_502_4@ //多点触摸事件响应函数 @H_502_4@ void onTouchesBegan(const std::vector& touches,Event *unused_event) { CCLOG("began"); } @H_502_4@ void onTouchesMoved(const std::vector& touches,Event *unused_event) { CCLOG("moved"); } @H_502_4@ void onTouchesEnded(const std::vector& touches,Event *unused_event) { CCLOG("ended"); } @H_502_4@ void onTouchesCancelled(const std::vector&touches,Event *unused_event) { CCLOG("cancelled"); } @H_502_4@ //
@H_502_4@
【鼠标事件】
EventListenerMouse,主要用于监听鼠标的点击、松开、移动、滚轮的事件。
鼠标事件监听器相关:
// @H_502_4@ static EventListenerMouse* create(); @H_502_4@ @H_502_4@ std::function onMouseDown; //按下鼠标,单击鼠标 @H_502_4@ std::function onMouseUp; //松开鼠标,按下的状态下松开 @H_502_4@ std::function onMouseMove;//移动鼠标,在屏幕中移动 @H_502_4@ std::function onMouseScroll;//滚动鼠标,滚动鼠标的滚轮 @H_502_4@ //
@H_502_4@ 使用举例: @H_502_4@ @H_502_4@
// @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //创建鼠标事件监听器 EventListenerMouse @H_502_4@ EventListenerMouse* mouseListenter = EventListenerMouse::create(); @H_502_4@ @H_502_4@ //鼠标事件响应函数 @H_502_4@ mouseListenter->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown,this); @H_502_4@ mouseListenter->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp,this); @H_502_4@ mouseListenter->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove,this); @H_502_4@ mouseListenter->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll,this); @H_502_4@ @H_502_4@ //添加鼠标事件监听器,事件响应处理委托给this @H_502_4@ dispatcher->addEventListenerWithSceneGraPHPriority(mouseListenter,this); @H_502_4@ @H_502_4@ @H_502_4@ //事件响应函数 @H_502_4@ void onMouseDown(Event* event) { CCLOG("Down"); } @H_502_4@ void onMouseUp(Event* event) { CCLOG("UP"); } @H_502_4@ void onMouseMove(Event* event) { CCLOG("MOVE"); } @H_502_4@ void onMouseScroll(Event* event) { CCLOG("Scroll"); } @H_502_4@ //
@H_502_4@
键盘事件】@H_502_4@
EventListenerKeyboard,主要用于监听键盘某个键的按下、松开的事件。
键盘事件监听器相关:
// @H_502_4@ static EventListenerKeyboard* create(); @H_502_4@ @H_502_4@ std::function onKeyPressed;//按下某键 @H_502_4@ std::function onKeyReleased; //松开某键 @H_502_4@ @H_502_4@ @H_502_4@ //键盘按键枚举类型 EventKeyboard::KeyCode @H_502_4@ //KeyCode的值对应的不是键盘的键值、也不是ASCII码,只是纯粹的枚举类型 @H_502_4@ //如: @H_502_4@ //EventKeyboard::KeyCode::KEY_A @H_502_4@ //EventKeyboard::KeyCode::KEY_1 @H_502_4@ //EventKeyboard::KeyCode::KEY_F1 @H_502_4@ //EventKeyboard::KeyCode::KEY_SPACE @H_502_4@ //EventKeyboard::KeyCode::KEY_ALT @H_502_4@ //EventKeyboard::KeyCode::KEY_SHIFT @H_502_4@ //
@H_502_4@ 使用举例: @H_502_4@ @H_502_4@
// @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //创建键盘按键事件监听器 @H_502_4@ EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create(); @H_502_4@ @H_502_4@ //绑定事件响应函数 @H_502_4@ keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed,this); @H_502_4@ keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased,this); @H_502_4@ @H_502_4@ //添加监听器 @H_502_4@ dispatcher->addEventListenerWithSceneGraPHPriority(keyboardListener,this); @H_502_4@ @H_502_4@ @H_502_4@ //事件响应函数 @H_502_4@ void onKeyPressed(EventKeyboard::KeyCode keyCode,Event* event) { @H_502_4@ if (EventKeyboard::KeyCode::KEY_J == keyCode) { @H_502_4@ CCLOG("Pressed: J"); @H_502_4@ } @H_502_4@ } @H_502_4@ void onKeyReleased(EventKeyboard::KeyCode keyCode,Event* event) { @H_502_4@ if (EventKeyboard::KeyCode::KEY_SPACE == keyCode) { @H_502_4@ CCLOG("Released: SPACE"); @H_502_4@ } @H_502_4@ } @H_502_4@ // @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@
@H_502_4@
【加速计事件】
EventListenerAcceleration,主要用于监听移动设备的所受重力方向感应事件。
重力感应来自移动设备的加速计,通常支持 (X,Y,Z) 三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。
@H_502_4@
1、加速计信息类Acceleration
该类中每个方向的加速度,大小都为一个重力加速度大小。
//加速计信息 @H_502_4@ class Acceleration @H_502_4@ { @H_502_4@ double x; double y; double z; @H_502_4@ }; @H_502_4@ // @H_502_4@ @H_502_4@
@H_502_4@
2、开启加速计感应@H_502_4@
在使用加速计事件监听器之前,需要先启用此硬件设备:
@H_502_4@
Device::setAccelerometerEnabled(true);
@H_502_4@
@H_502_4@
3、加速计监听器相关
// @H_502_4@ static EventListenerAcceleration* create(const std::function& callback); @H_502_4@ @H_502_4@ std::function onAccelerationEvent; @H_502_4@ //
@H_502_4@ 4、使用举例 @H_502_4@
// @H_502_4@ //标签: 显示加速计信息 @H_502_4@ label = Label::createWithTTF("no used","Marker Felt.ttf",12); @H_502_4@ label->setPosition(visibleSize / 2); @H_502_4@ this->addChild(label); @H_502_4@ @H_502_4@ @H_502_4@ //小球: 可视化加速计 @H_502_4@ ball = Sprite::create("ball.png"); @H_502_4@ ball->setPosition(visibleSize / 2); @H_502_4@ this->addChild(ball); @H_502_4@ @H_502_4@ @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //需要开启移动设备的加速计 @H_502_4@ Device::setAccelerometerEnabled(true); @H_502_4@ @H_502_4@ //创建加速计事件监听器 @H_502_4@ auto accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAccelerationEvent,this)); @H_502_4@ @H_502_4@ //添加加速计监听器 @H_502_4@ dispatcher->addEventListenerWithSceneGraPHPriority(accelerationListener,this); @H_502_4@ @H_502_4@ @H_502_4@ //事件响应函数 @H_502_4@ void HelloWorld::onAccelerationEvent(Acceleration* acceleration,Event* event) @H_502_4@ { @H_502_4@ char s[100]; @H_502_4@ sprintf(s,"X: %f; Y: %f; Z:%f; ",acceleration->x,acceleration->y,acceleration->z); @H_502_4@ label->setString(s); @H_502_4@ @H_502_4@ //改变小球ball的位置 @H_502_4@ float x = ball->getPositionX() + acceleration->x * 10; @H_502_4@ float y = ball->getPositionY() + acceleration->y * 10; @H_502_4@ Vec2 pos = Vec2(x,y); @H_502_4@ pos.clamp(ball->getContentSize() / 2,Vec2(288,512) - ball->getContentSize() / 2); @H_502_4@ ball->setPosition(pos); //设置位置 @H_502_4@ } @H_502_4@ //
@H_502_4@
5、实际效果
在电脑上看不出效果,需要移植到手机上,才能看到加速计的效果
@H_502_4@
自定义事件】
以上是系统自带的事件类型,事件由系统内部自动触发,如 触摸屏幕,键盘响应等。
EventListenerCustom 自定义事件,它不是由系统自动触发,而是人为的干涉。
@H_502_4@
1、创建自定义监听器
@H_502_4@ @H_502_4@
// @H_502_4@ //eventName : 监听器名字 @H_502_4@ //callback: 监听器函数 @H_502_4@ static EventListenerCustom* create(const std::string& eventName,const std::function& callback); @H_502_4@ //
@H_502_4@
2、分发自定义事件
自定义的事件监听器,需要通过手动的方式,将事件分发出去。
通过 EventCustom(string eventName); 来获取自定义监听器。
通过 dispatcher->dispatchEvent(&event); 来手动将事件分发出去。
// @H_502_4@ EventCustom event("your_event_type"); @H_502_4@ dispatcher->dispatchEvent(&event); @H_502_4@ //
@H_502_4@ 3、使用举例 @H_502_4@
// @H_502_4@ //获取事件分发器 @H_502_4@ auto dispatcher = Director::getInstance()->getEventDispatcher(); @H_502_4@ @H_502_4@ //创建自定义事件监听器 @H_502_4@ //监听器名字: "custom_event" @H_502_4@ //事件响应函数: HelloWorld::onCustomEvent @H_502_4@ auto customListener = EventListenerCustom::create("custom_event",CC_CALLBACK_1(HelloWorld::onCustomEvent,this)); @H_502_4@ @H_502_4@ //添加自定义事件监听器,优先权为1 @H_502_4@ dispatcher->addEventListenerWithFixedPriority(customListener,1); @H_502_4@ @H_502_4@ //手动分发监听器的事件,通过dispatchEvent @H_502_4@ EventCustom event = EventCustom("custom_event"); @H_502_4@ dispatcher->dispatchEvent(&event); @H_502_4@ @H_502_4@ @H_502_4@ //事件响应函数 @H_502_4@ void HelloWorld::onCustomEvent(EventCustom* event) @H_502_4@ { @H_502_4@ CCLOG("onCustomEvent"); @H_502_4@ } @H_502_4@ //
@H_502_4@
4、说明
  • 每个自定义的事件监听器,都有一个监听器名字eventName。
  • 需要手动通过 dispatcher->dispatchEvent(&event); 来手动将事件分发出去。
  • 可以通过 dispatcher->dispatchCustomEvent(,); 来给自定义事件监听器绑定一个用户数据。

猜你在找的Cocos2d-x相关文章