Cocos2dx Engine 9-register the event listener

Source: Internet
Author: User
Tags custom name

Cocos2dx Engine 9-register the event listener

1. event handling methods

The following is the registration part of the normal Click Event listener (EventListener). To register EventListener, you must implement the onTouchBegan method to implement the onTouchEnded and onTouchCancelled methods. Of course, if you want to listen for touch (Mouse) the onTouchMoved method must be implemented for the mobile (drag) listener. Here, the Lambda method is used to implement the onTouchBegin method;

Auto event = EventListenerTouchOneByOne: create (); event-> onTouchBegan = [sprite] (Touch * touch, Event * event) {if (sprite-> getBoundingBox (). containsPoint (touch-> getLocation ())){...... Return true; // The returned value is critical at this time; true will continue to call the following onTouchEnded method; false will not call the onTouchEnded method} return false ;}; event-> onTouchEnded = [this] (Touch * touch, Event * event ){......};

Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (event, sprite); // register EventListener

In the Cocos2dx event processing system, the Node where the event is sent is not distinguished. No matter where you click the screen (of course, clicking the Black edge will only print the error message), Cocos2dx will execute all the registered EventListener processing methods. Therefore, you need to add

If (sprite-> getBoundingBox (). containsPoint (touch-> getLocation ()))

To determine whether to register EventListener;

2,EventListenerRegistration Method

In the above example, EventListener registered using the addEventListenerWithSceneGraphPriority method is also supported in Cocos2dx.

(1) addCustomEventListener: Add a custom EventListener

(2) addEventListenerWithFixedPriority add EventListener with priority

(3) addEventListenerWithSceneGraphPriority: Add EventListener in Node binding mode. The priority is 0 and the priority is the highest.

AddCustomEventListener can define EventListener by yourself and manually distribute the event when appropriate. In this way, all user-defined EventListener using this ID will be called. This method can be used to achieve broadcast effect;

The EventListener registered by addEventListenerWithFixedPriority will exist. You know to manually remove this EventListener;

AddEventListenerWithSceneGraphPriority will be bound to the Node when registering EventListener, And the Listern bound to the Node will be removed from the Node's destructor (called when the Node is deleted from the memory; it is better to use this method in the node. When Scene is switched, all unnecessary EventListener will be removed;

The following describes how to add these three methods.

EventListenerCustom*EventDispatcher::addCustomEventListener(const std::string &eventName, conststd::function
 
  & callback){    EventListenerCustom *listener =EventListenerCustom::create(eventName, callback);    addEventListenerWithFixedPriority(listener,1);    return listener;}
 

In addCustomEventListener:

(1) Use a custom name, callback function, and a new custom listener EventListener

(2) Add a custom EventListener

As the name suggests, the function of this method is to add a custom EventListener, and callback is the callback function. You need to manually call ctor: getInstance ()-> getEventDispatcher ()-> dispatchCustomEvent (...) Or

Director: getInstance ()-> getEventDispatcher ()-> dispatchEvent (...) To trigger

voidEventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, intfixedPriority){       if (!listener->checkAvailable())        return;    listener->setAssociatedNode(nullptr);   listener->setFixedPriority(fixedPriority);    listener->setRegistered(true);    listener->setPaused(false);    addEventListener(listener);}

In the addEventListenerWithFixedPriority method:

(1) determine whether EventListener is available

(2) set the associated node to null, set the priority, set the registered, pause flag

(3) Add the EventListener to the EventListener list.

VoidEventDispatcher: addEventListenerWithSceneGraphPriority (EventListener * listener, Node * node) {// if _ onEvent = false, there is no default callback function if (! Listener-> checkAvailable () return; listener-> setAssociatedNode (node); // EventListener belongs to Node listener-> setFixedPriority (0); listener-> setRegistered (true ); addEventListener (listener); // Add and click EventListener}

In addEventListenerWithSceneGraphPriority

(1) determine whether EventListener is available

(2) Set EventListener to join the node and set FixedPriority to 0.

(3) add EventListener

3,EventListenerRegister

VoidEventDispatcher: addEventListener (EventListener * listener) {if (_ inDispatch = 0) {forceAddEventListener (listener); // Add Listener} else {_ toAddedListeners. push_back (listener); // temporarily add EventListener to _ toAddedListeners} listener-> retain (); // memory management part, reference count + 1}

In the addEventListener method

(1) determine whether an event is being distributed or how it is handled

(2) Add EventListener to the EventListener list if it is used to distribute events or execute event processing methods; otherwise, temporarily add EventListener to toAddedListeners.

Although Cocos2dx is a single-thread loop rendering, EventListener, such as touch screen clicks and acceleration sensors, is processed in the new thread. If the processor is multi-core (most mobile phones are now multi-core) multiple Threads may execute at the same time. When an EventListener is added, the EventListener will be added to _ toAddedListeners when an event is being distributed or the event processing method is executed, after the event is processed, add EventListener in _ toAddedListeners to the EventListener list. The specific implementation will be described in the following event distribution.

Listener: forceAddEventListener (EventListener * listener) {EventListenerVector * listeners = nullptr; EventListener: ListenerID listenerID = listener-> getListenerID (); auto itr = _ listenerMap. find (listenerID); // search for the EventListener list based on the EventListener type if (itr = _ listenerMap. end () {// This type of EventListener is added to listeners = new EventListenerVector () for the first time; // creates the EventListener LIST _ listenerMap. insert (std: make _ Pair (listenerID, listeners);} else {listeners = itr-> second;} listeners-> push_back (listener); // Add EventListener if (listener-> getFixedPriority () = 0) {setDirty (listenerID, DirtyFlag: SCENE_GRAPH_PRIORITY); auto node = listener-> getAssociatedNode (); CCASSERT (node! = Nullptr, "Invalidscene graph priority! "); AssociateNodeAndEventListener (node, listener); if (node-> isRunning () {resumeEventListenersForTarget (node);} else {setDirty (listenerID, DirtyFlag :: FIXED_PRIORITY );}}

In the forceAddEventListener method

(1) Search for the EventListener list based on the EventListener type. If this type of EventListener is added for the first time, create an EventListener list of this type.

(2) Add the EventListener to the EventListener class table. When adding EventListener, the system first determines whether _ fixedPriority is 0. If it is 0, it is added to _ sceneGraphListeners. Otherwise, it is added to _ fixedListeners.

(3) set the dirty data mark corresponding to the listenerID. When _ fixedPriority is 0, bind EventListener to the Node registered with EventListener and insert it into _ nodeListenersMap;

After EventListener registration is complete, we are concerned with event distribution, and the next article will introduce the event distribution mechanism of the Cocos2dx-3.2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.