Cocos2d-x event distribution mechanism-touch event listening, cocos2d-x Distribution

Source: Internet
Author: User

Cocos2d-x event distribution mechanism-touch event listening, cocos2d-x Distribution

Cocos2d-x 3.0 has been out for a long time, also has been used 3.0 to write a few games, feel 3.0 event trigger mechanism is too good, with here to sum up, it is also a kind of memory and deep understanding of knowledge.

In the event distribution mechanism of 3.0, you only need to create an event listener to implement various trigger logic, and then add it to the event distributor _ eventDispatcher, all event listeners are managed by the distributor to Complete Event Response.

Event Listeners include the following types:

1. EventListenerTouch)

2. EventListenerKeyboard (Keyboard RESPONSE event)

3. EventListenerMouse (mouse RESPONSE event)

4. EventListenerAcceleration (events recorded during acceleration)

5. EventListenerCustom (Custom Event)


_ EventDispatcher consists of three parts:

1. EventDispatcher

2. event types: EventTouch, EventKeyboard, etc.

3. Event listener (five types listed above)

The listener implements various trigger logics. The event distributor distributes all registered events and calls the response type listener to respond to the events.


This article describes how to monitor EventListenerTouch for the most touch events in the game.

In 3.0, the touch listener becomes very simple. You only need to register the event listener for the response and assign the callback function for the response to each response function of the event listener, finally, register the implemented event listener to the event distributor _ eventDispatcher to implement the logical functions of each callback function.

Step 1: wear the event listener and register it to the time distributor. It is best to implement this step in the onEnter () function.

// Register auto touchListener = EventListenerTouchOneByOne: create (); // create a single point of touch event listener touchListener-> onTouchBegan = CC_CALLBACK_2 (GameLayer: onTouchBegan, this ); // touch start touchListener-> onTouchMoved = CC_CALLBACK_2 (GameLayer: callback, this); // touch mobile touchListener-> onTouchEnded = CC_CALLBACK_2 (GameLayer: onTouchEnded, this ); // touch end _ eventDispatcher-> addEventListenerWithSceneGraphPriority (touchListener, this; // register the distributor

Step 2: implement the three callback functions in the listener

Bool GameLayer: onTouchBegan (Touch * touch, Event * event) {// implement your own logic code return true;} void GameLayer: onTouchMoved (Touch * touch, Event * event) {// implement your own logic code} void GameLayer: onTouchEnded (Touch * touch, Event * event) {// implement your own logic code}
If the onTouchBegan () function returns true, the response event will not be passed to the lower layer. It will only respond to the Began, Moved, and Ended functions in this layer. If false is returned, the subsequent Moved and Ended functions of the current layer will not be triggered, but the event will be passed to the lower layer.

You can also use

TouchListener-> setSwallowTouches (true); // do not pass the touch down
To set whether to pass down the touch event.

_ EventDispatcher is a Node attribute used to manage the distribution of all events on the current Node (scenario, layer, Genie, etc. It is a reference of Singleton mode. In the Node constructor, it is obtained through Director: getInstance ()-> getEventDispatcher.

When an event listener needs to be reused, you need to use the clone () method to create a new clone, because the addEventListenerWithSceneGraphPriorith or addEventListenerWithFixedPriority method is used for registration, A registered tag will be added to the currently used event listener, so that it cannot be added multiple times. In addition, when FixedPriority listeners is used, you need to manually remove it after adding it. SceneGraphyPriority listener is bound to Node and will be removed from Node destructor.

Remove a registered listener

_eventDispatcher->removeEventListener(listener);
You can also directly call the removeAllListeners function, but it is not recommended to do so. After this, all event listeners will not respond.


Well, This article focuses on the event touch mechanism. In the next article, we will talk about the event accelerator.


Cocos2d-x, how to implement the genie not responding to other touch events before an action ends

This problem is common, that is, you click it once and let it jump. Wait until it falls down, click again, and then jump again. Clicking during the animation will not jump.
Generally, in game development, we are used to using a bool en variable for control. You should first click the event, that is, set en to false when the genie runAction, in addition, jumpBy is recommended for Skip animations. A callback is added to this callback to set en to true when the animation ends.
We recommend that you take a look at damo. Thank you ~ Wish you growth.

Cocos2d-x screen Click Event

You need to reload the virtual void registerWithTouchDispatcher (); Function
Implementation
Director: getInstance ()-> getTouchDispatcher ()-> addTargetedDelegate (this, Menu: HANDLER_PRIORITY-1, true); I am 3.0
2. x is
CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );

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.