Event distribution mechanism
Respect for the original: http://cn.cocos2d-x.org/tutorial/show?id=2154
What is the event distribution mechanism
Eventdispatch is a mechanism for responding to user events.
Basic concepts:
- Event listeners encapsulate the code for event handling;
- The event Scheduler notifies listeners of user events;
- The event object contains information about the event.
5 Types of event listeners
EventListenerTouch
-Respond to touch events
EventListenerKeyboard
-Respond to keyboard events
EventListenerAcceleration
-Response to an accelerometer event
EventListenMouse
-Respond to mouse events
EventListenerCustom
-Respond to custom events
Fixedpriority vs Scenegraphpriority
- The Eventdispatcher event distribution mechanism uses precedence to determine which listener to trigger at the start of an event.
- Fixedpriority is an integer value. Event listeners with a lower priority value are processed before the event listener with higher priority values.
- Scenegraphpriority is a pointer to node. An event listener with a higher node Z-order value (drawn at the top) receives an event before the event listener with a lower node Z-order value (drawn at the bottom). This will ensure that the touch events are passed back and forward as you would expect.
Remember the second chapter, our discussion of the scene map? Such as:
, when we use scenegraphpriority, it is actually executed in reverse order, I, H, G, F, E, D, C, B, A. If an event is triggered, H will first check the event, swallow the event, or pass the event to I. In the same vein, I will either swallow the event or pass it to G and execute it sequentially until there are no events to swallow or no event response.
Touch events
In the hand tour, the most important event is the touch event. They are easily created to provide common functionality. First we need to be clear about what a touch event is. When you touch the screen of a mobile device, the screen receives the touch behavior and checks where you touch and determines what you touch. Then your touch behavior will be answered. What you touch may not be a response object, but it's probably something underneath it. Typically, the priority is assigned to the touch event, with the highest priority being the first response. The following code creates a basic touch event listener:
//Create A "one by one" touch Event listener//(Processes one touch at a time)Auto Listener1 =eventlistenertouchonebyone::create ();//trigger when to push downListener1->ontouchbegan = [] (touch* Touch, event*Event){ //Your code return true;//If you are consuming it}; //trigger when moving touchlistener1->ontouchmoved = [] (touch* Touch, event*Event){ //Your code}; //Trigger when the uplistener1->ontouchended = [=] (touch* Touch, event*Event){ //Your code}; //ADD Listener_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener1, This);
As you can see, when you use the Touch event listener, there are three different events for you to manipulate, each of which is not the same time period that the event is called.
Annexation Event (swallowing events)
If you have a listener and want an object to accept the event, you must swallow it. In other words, annexing it prevents it from passing high priority to other low-priority objects. This is easy to achieve:
//When "Swallow touches" is true, then returning & #039;true& #039;//Ontouchbegan method would "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 return true;};
Creating keyboard events
For desktop games, you will find that the keyboard mechanism is very useful. Cocos2d-x supports keyboard events. As with the touch events described above, keyboard events are also easy to create:
//Creating a keyboard event listenerAuto 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 prototypevoidKeyboardtest::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);}
Create an Accelerometer Event
Some mobile devices are equipped with accelerometers. The accelerometer is a sensor that can measure gravity and direction changes. For example, move your phone back and forth to simulate balance. Cocos2d-x also supports these events and is simple to create. Before using the accelerometer event, you need to activate the event on the device:
device::setaccelerometerenabled (true ); // Creating an accelerometer event auto listener = eventlisteneracceleration:: Create (Cc_callback_2 (accelerometertest::onacceleration, this ->addeventlistenerwithscenegraphpriority (Listener, This ); // implementation of the accelerometer callback function prototype void accelerometertest::onacceleration (acceleration* acc, event* event // Processing logic here }
Creating mouse Events
Mouse events are always supported in Cocos2d-x.
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; stringstr ="Mouse down detected, Key:"; STR+ = Tostr (e->Getmousebutton ()); // ...}voidMousetest::onmouseup (Event *Event) {Eventmouse* E = (eventmouse*)Event; stringstr ="Mouse up detected, Key:"; STR+ = Tostr (e->Getmousebutton ()); // ...}voidMousetest::onmousemove (Event *Event) {Eventmouse* E = (eventmouse*)Event; stringstr ="mouseposition X:"; STR= str + TOSTR (E->getcursorx ()) +"Y:"+ TOSTR (e->getcursory ()); // ...} voidMousetest::onmousescroll (Event *Event) {Eventmouse* E = (eventmouse*)Event; stringstr ="Mouse Scroll detected, X:"; STR= str + TOSTR (E->GETSCROLLX ()) +"Y:"+ TOSTR (e->getscrolly ()); // ...}
Registering Events with Allocators
Events can be easily registered using the event allocator. For example, the touch event monitor in the above article:
// ADD Listener_eventdispatcher->addeventlistenerwithscenegraphpriority (listener1,sprite1);
It is important to note that each object can only register one touch event. If multiple objects need to use the same listener, you need to use the Clone () method.
// ADD listenereventdispatcher->addeventlistenerwithscenegraphpriority (listener1,sprite1); // ADD the same listener to multiple objects. _eventdispatcher->addeventlistenerwithscenegraphpriority (listener1->clone (), Sprite2); _ Eventdispatcher->addeventlistenerwithscenegraphpriority (Listener1->clone (), sprite3);
Removing events from the allocator
You can remove an existing listener by using the following method:
_eventdispatcher->removeeventlistener (listener);
Although this may seem special, the built-in Node
object usage event distribution mechanism is the same as the way we speak. Menu
For example, when you click with MenuItems
attributes Menu
, you are assigned to an event. You can also use the RemoveEventListener method for built-in Node
objects.
Event distribution mechanism