cocos2d-x中的事件分发机制

来源:互联网 发布:不定积分 软件 编辑:程序博客网 时间:2024/04/27 23:04

什么是事件分发机制

EventDispatch是响应用户事件的一种机制。

基本概念:

  • 事件监听器封装了事件处理的代码;
  • 事件调度器通知用户事件的监听器;
  • 事件对象包含了关于事件的信息。

事件监听器的5种类型

EventListenerTouch - 响应触摸事件

EventListenerKeyboard - 响应键盘事件

EventListenerAcceleration - 响应加速度计的事件

EventListenMouse - 响应鼠标事件

EventListenerCustom - 响应自定义事件

FixedPriority vs SceneGraphPriority

  • EventDispatcher事件分发机制使用优先级来决定在事件开始时触发哪一个监听器。
  • FixedPriority是一个整数值。优先级值较低的事件监听器会在优先级值较高的事件监听器之前处理。
  • SceneGraphPriority是一个指向Node的指针。节点z-order值较高(在顶端绘制)的事件监听器在节点z-order值较低(在底端绘制)的事件监听器之前接受到事件。这将确保触摸事件能像你所期望的那样,从前往后依次传递。

in order walk

如图,当我们使用SceneGraphPriority时,其实是按倒序执行的,I, H, G, F, E, D, C, B, A。如果一个事件被触发,H将会首先检查这个事件,吞并这个事件或者将事件传递给I。同理,I将吞并这个事件或者将事件传递给G,依次执行,直到没有可吞并的事件或者没有事件响应为止。

触摸事件

在手游中,最重要的事件是触摸事件。它们很容易被创建来提供通用的功能。首先我们要明确什么是触摸事件。当你触摸移动设备的屏幕时,屏幕会接收到这个触摸行为,并检查你触摸了哪里以及决定你触摸到了什么。然后你的触摸行为就会被响应。你所触摸的或许不是响应对象,但很有可能是它下面的东西。通常会给触摸事件分配优先级,优先级最高的就是被先响应的。以下代码创建了一个基本的触摸事件监听器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//  Create a "one by one" touch event listener
// (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne::create();
 
// trigger when you push down
listener1->onTouchBegan = [](Touch* touch, Event* event){
// your code
returntrue;// if you are consuming it
};
 
// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
// your code
};
 
// trigger when you let up
listener1->onTouchEnded = [=](Touch* touch, Event* event){
// your code
};
 
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,this);

正如你所见,当你使用触摸事件侦听器时,这儿有三个不同的事件供你操作,它们每一个事件被调用的时间段是不相同的。

  • 按下时会触发onTouchBegan函数。

  • 按下时移动对象会触发onTouchMoved函数。

  • 停止触摸时会触发onTouchEnded函数。

吞并事件(Swallowing Events)

如果你有一个监听器并且希望某个对象能接受到事件,那么你必须吞并它。换言之,吞并它能避免它把高优先级传递给其他低优先级对象。这很容易实现:

1
2
3
4
5
6
7
8
9
10
11
// When "swallow touches" is true, then returning 'true' from the
// onTouchBegan method will "swallow" the touch event, preventing
// other listeners from using it.
listener1->setSwallowTouches(true);
 
// you should also return true in onTouchBegan()
listener1->onTouchBegan = [](Touch* touch, Event* event){
    // your code
 
    returntrue;
};

创建键盘事件

对于桌面游戏,你会发现键盘机制是非常有用的。Cocos2d-x支持键盘事件。像上面所讲的触摸事件一样,键盘事件也很容易创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);
 
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
 
// Implementation of the keyboard event callback function prototype
voidKeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
    log("Key with keycode %d pressed", keyCode);
}
 
voidKeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
    log("Key with keycode %d released", keyCode);
}

创建加速计事件

很些移动设备都配备了加速度计。加速计是一个传感器,可以测量重力和方向上的变化。例如,来回移动你的电话来模拟平衡。Cocos2d-x也支持这些事件并且创建起来很简单。在使用加速计事件之前,你需要在设备上激活这个事件:

1
2
3
4
5
6
7
8
9
10
11
Device::setAccelerometerEnabled(true);
// creating an accelerometer event
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(
AccelerometerTest::onAcceleration,this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
 
// Implementation of the accelerometer callback function prototype
voidAccelerometerTest::onAcceleration(Acceleration* acc, Event* event)
{
    //  Processing logic here
}

创建鼠标事件

Cocos2d-x中一直支持鼠标事件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
_mouseListener = EventListenerMouse::create();
_mouseListener->onMouseMove = CC_CALLBACK_1(MouseTest::onMouseMove, this);
_mouseListener->onMouseUp = CC_CALLBACK_1(MouseTest::onMouseUp, this);
_mouseListener->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown, this);
_mouseListener->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll, this);
 
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener,this);
 
voidMouseTest::onMouseDown(Event *event)
{
    EventMouse* e = (EventMouse*)event;
    string str = "Mouse Down detected, Key: ";
    str += tostr(e->getMouseButton());
    // ...
}
 
voidMouseTest::onMouseUp(Event *event)
{
    EventMouse* e = (EventMouse*)event;
    string str = "Mouse Up detected, Key: ";
    str += tostr(e->getMouseButton());
    // ...
}
 
voidMouseTest::onMouseMove(Event *event)
{
    EventMouse* e = (EventMouse*)event;
    string str = "MousePosition X:";
    str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
    // ...
}
 
voidMouseTest::onMouseScroll(Event *event)
{
    EventMouse* e = (EventMouse*)event;
    string str = "Mouse Scroll detected, X: ";
    str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
    // ...
}

使用分配器注册事件

使用事件分配器可以很容易的注册时间。以上文的触摸事件监视器为例:

1
2
3
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,
sprite1);

值得注意的是,每个对象都只能注册一个触摸事件。如果多个对象需要使用相同的监听器,你需要使用clone()方法。

1
2
3
4
5
6
7
8
9
10
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,
sprite1);
 
// Add the same listener to multiple objects.
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
 sprite2);
 
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
 sprite3);

从分配器中移除事件

使用如下方法可以移除一个已有的监听器:

1
_eventDispatcher->removeEventListener(listener);

尽管这看起来很特殊,但是内置的Node对象使用事件分发机制与我们所讲的方式是相同的。以Menu为例,当点击带有MenuItems属性的 Menu时,你就已经分配到了一个事件。同样也可对内置的Node对象使用removeEventListener方法。

0 0
原创粉丝点击