Cocos2xd__用户事件

梦想的初衷 提交于 2020-03-21 03:17:59

3 月,跳不动了?>>>

事件类 Event:class Event : public Ref

其子类:

  1. EventTouch:触摸事件

  2. EventMouse:鼠标事件

  3. EventKeyboard:键盘事件

  4. EventAcceleration:加速度事件

  5. EventCustom:自定义事件

 

事件源:

  精灵、层、菜单等节点对象。

 

事件处理者:EventListener

  class EventListener : public Ref

  其子类:

  1. EventListenerTouchOneByOne:单点触摸事件监听器

  1. EventListenerTouchAllAtOnce:多点触摸事件监听器

  2. EventListenerMouse:鼠标事件监听器

  3. EventListenerKeyboard:键盘事件监听器

  4. EventListenerAcceleration:加速度事件监听器

  5. EventListenerCustom:自定义事件监听器

 

可以看处事件监听器与事件具有对应关系。即键盘事件只能由键盘监听器处理。

 

事件分发器 EventDispatcher 负责注册、注销监听器和事件分发。采用的单例设计。

可以使用 _eventDispatcher 注册事件和注销事件,如:

   _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, node); 

 

看看 EventDispatcher 提供的事件管理方法:

一、注册事件:

  1. void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)

  2. void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)

  3. EventListenerCustom* addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback)

二、注销事件:

  1. void removeEventListener(EventListener* listener)

  2. void removeAllEventListeners()

  3. void removeCustomEventListeners(const std::string& customEventName)

  4. void removeEventListenersForTarget(Node* target, bool recursive /* = false */)

  5. void removeEventListenersForType(EventListener::Type listenerType)

 

一、 触摸事件:

触摸事件可分为三个阶段:按下、移动、抬起

触摸事件有两个监听器:EventListenerTouchOneByOne、EventListenerTouchAllAtOnce

EventListenerTouchOneByOne 中触摸事件响应属性:

  1. std::function<bool(Touch*, Event*)> onTouchBegan:触屏时回调,若返回 true 则可继续回调 2,3,否则不回调。

  2. std::function<void(Touch*, Event*)> onTouchMoved:在屏幕移动时回调

  3. std::function<void(Touch*, Event*)> onTouchEnded:离开屏幕时回调

  4. std::function<void(Touch*, Event*)> onTouchCancelled:单点触摸事件被取消时回调

  注:std::function 是一种通用的函数封装,请参见 C++。

EventListenerTouchAllAtOnce 中触摸事件响应属性:

  1. std::function<void(const std::vector<Touch*>&, Event*)> onTouchBegan:多点触屏时回调,若返回 true 则可继续回调 2,3,否则不回调。

  2. std::function<void(const std::vector<Touch*>&, Event*)> onTouchMoved:多点在屏幕移动时回调

  3. std::function<void(const std::vector<Touch*>&, Event*)> onTouchEnded:多点离开屏幕时回调

  4. std::function<void(const std::vector<Touch*>&, Event*)> onTouchCancelled:多点触摸事件被取消时回调

例如(单点触摸事件):

//  Create a "one by one" touch event listener
// (processes one touch at a time)
auto touchListener = EventListenerTouchOneByOne::create();

// trigger when you push down
touchListener->onTouchBegan = [](Touch* touch, Event* event) {
    // your code
    return true; // if you are consuming it
};

// trigger when moving touch
touchListener->onTouchMoved = [](Touch* touch, Event* event) {
    // your code
};

// trigger when you let up
touchListener->onTouchEnded = [=](Touch* touch, Event* event) {
    // your code
};

// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

当然回调函数也可以这样:

   touchListener->onTouchBegan = CC_CALLBACK_2(TestScene::touchBegan, this); 

 

二、键盘事件:

键盘事件响应属性:

  1. std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed

  2. std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased

  EventKeyboard::KeyCode 自然是按键 Code,看看一些常用的按键 Code:

  1. A~Z:EventKeyboard::KeyCode::KEY_A ~ EventKeyboard::KeyCode::KEY_Z

  2. 0~9:EventKeyboard::KeyCode::KEY_0 ~ EventKeyboard::KeyCode::KEY_9

  3. F1~F12:EventKeyboard::KeyCode::KEY_F1 ~ EventKeyboard::KeyCode::KEY_F12

  4. ESC:EventKeyboard::KeyCode::KEY_ESCAPE

  5. TAB、SHIFT、CTRL、ALT 分别为(省略 EventKeyboard::KeyCode::):

    KEY_TAB、KEY_SHIFT(KEY_LEFT_SHIFT、KEY_RIGHT_SHIFT)、KEY_CTRL(KEY_LEFT_CTRL、KEY_RIGHT_CTRL)、KEY_ALT(KEY_LEFT_ALT、KEY_RIGHT_ALT)

  6. 空格:KEY_SPACE

  其他的参见源码。

演示:

auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode kcode, Event*)
{
    cocos2d::log("onKeyPressed:keyCode = %d", kcode);
};
listener->onKeyReleased = [](EventKeyboard::KeyCode kcode, Event*)
{
    cocos2d::log("onKeyReleased:keyCode = %d", kcode);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

 

三、鼠标事件:

鼠标事件响应属性:

  1. std::function<void(EventMouse* event)> onMouseDown:按下

  2. std::function<void(EventMouse* event)> onMouseUp:抬起

  3. std::function<void(EventMouse* event)> onMouseMove:移动

  4. std::function<void(EventMouse* event)> onMouseScroll:滚轮滚动

如:

auto listener = EventListenerMouse::create();
listener->onMouseDown = [](Event*)
{
    cocos2d::log("onMouseDown");
};
listener->onMouseUp = [](Event*)
{
    cocos2d::log("onMouseUp");
};
listener->onMouseMove = [](Event*)
{
    cocos2d::log("onMouseMove");
};
listener->onMouseScroll = [](Event*)
{
    cocos2d::log("onMouseScroll");
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

相信你已经发现了,如何知道具体是鼠标的哪个键响应的呢?

listener->onMouseDown = [](Event* event)
{
    auto eventMouse = (EventMouse*)event;
    cocos2d::log("onMouseDown, key = %d", eventMouse->getMouseButton());
};

  getMouseButton() 方法就是用来获取的具体按键的。

  

 

 如何获取鼠标坐标呢?

listener->onMouseDown = [](Event* event)
{
    auto eventMouse = (EventMouse*)event;
    cocos2d::log("onMouseDown, (px, py) = (%f, %f)", eventMouse->getCursorX(), eventMouse->getCursorY());
};

依次在窗口四个角和中心点击看看:

  

 

 

 

待续......

//

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!