Cocos2d-x Touch Event Processing Mechanism

Source: Internet
Author: User

 


Two touch event processing mechanisms are available in the Cocos2d-x: CCStandardTouchDelegate and CCTargetedTouchDelegate.

 

I. How to Use
0. By default, the CCLayer does not start the touch event. Therefore, you must enable this-> setTouchEnabled (true) in the initialization function.
1. First, you must register the touch event processing mechanism in the registerWithTouchDispatcher () method.
[Cpp]
Void HelloWorld: registerWithTouchDispatcher ()
{
// Standard Touch
CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addStandardDelegate (this, 0 );
// Targeted Touch
// CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
}

Void HelloWorld: registerWithTouchDispatcher ()
{
// Standard Touch
CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addStandardDelegate (this, 0 );
// Targeted Touch
// CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
}

PS: the default registerWithTouchDispatcher Implementation of CCLayer is registered as CCStandardTouchDelegate. In addition, you must call registerWithTouchDispatcher instead of registerWithTouchDispatcher.

This-> setTouchEnabled (true );

 


Note: When we use the touch event mechanism for registration, we obviously need to remove it after use. In fact, this part of content does not need to be manually processed, the removed operation is automatically executed at OnExit, so we don't need to worry about it!

 


2. Implement callback Functions

The callback function for processing touch events is specified in both implementation mechanisms. Therefore, after you register different processing mechanisms, You need to implement the callback function for the response.

 


(1) Standard Touch

In CCStandardTouchDelegate, there are four callback functions:

CCStandardTouchDelegate default event
Virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent); process the pressing event
Virtual void ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent); handles pressing and moving events
Virtual void ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent); handles release events
Virtual void ccTouchesCancelled (CCSet * pTouches, CCEvent * pEvent); process interrupt events
① For the parameters in these callback functions, the received touch is of the CCSet type, indicating that it is a multi-point touch. Then you can implement multi-touch processing. If you want to implement multi-touch, you must first


[Cpp]
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
Set in method:


[Cpp]
// Set multi-point touch
[_ GlView setMultipleTouchEnabled: YES];

// Set multi-point touch
[_ GlView setMultipleTouchEnabled: YES];

Then, you can perform multi-touch processing in the callback function:


[Cpp]
Void HelloWorld: ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent)
{
CCLog ("tap count = % d", pTouches-> count ());

For (CCSetIterator iterTouch = pTouches-> begin (); iterTouch! = PTouches-> end (); iterTouch ++ ){

CCTouch * pCurTouch = (CCTouch *) (* iterTouch );
CCPoint point = pCurTouch-> getLocation ();
CCLog ("% f, % f", point. x, point. y );
}
}

Void HelloWorld: ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent)
{
CCLog ("tap count = % d", pTouches-> count ());

For (CCSetIterator iterTouch = pTouches-> begin (); iterTouch! = PTouches-> end (); iterTouch ++ ){

CCTouch * pCurTouch = (CCTouch *) (* iterTouch );
CCPoint point = pCurTouch-> getLocation ();
CCLog ("% f, % f", point. x, point. y );
}
}

 

 

 

(2) Target Touch

CCTargetedTouchDelegate contains the following four callback functions:


CCTargetedTouchDelegate
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); process user-pressed events. true indicates continued processing; otherwise false.
Virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent); handles pressing and moving events
Virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent); handles release events
Virtual void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent); process interrupt events
CCTargetedTouchDelegate differs greatly from CCStandardTouchDelegate.

 

First, the parameters in the CCTargetedTouchDelegate callback receive not a set of Touch events (CCSet), but a single Touch event, the cocos2d-x will split the multi-Touch into a single Touch event and then callback. That is, the event parameter is not a set, but only one touch point is input at a time.

 


Second, the user must implement the ccTouchBegan function. If a user needs to continue tracking when the message is pressed, ccTouchBegin returns true. Otherwise, interfaces such as ccTouchMoved and ccTouchEnded will not be called. That is, the ccTouchBegan method returns a Boolean value indicating whether to capture the touch point. Only the touch points captured in this method will continue to trigger the other three events, otherwise, other events of this touch point will be ignored.

 


Third, note that


[Cpp]
Void CCTouchDispatcher: addTargetedDelegate (CCTouchDelegate * pDelegate, int nPriority, bool bSwallowsTouches)

Void CCTouchDispatcher: addTargetedDelegate (CCTouchDelegate * pDelegate, int nPriority, bool bSwallowsTouches)


In the addTargetedDelegate method, the first two parameters correspond to the touch receiving object and priority respectively. The priority is an integer parameter. The lower the value, the higher the priority, the earlier the touch event is obtained. Generally, to obtain a higher priority, you can specify it as a negative number.


The third parameter is interesting, indicating whether to "Swallow" a touch. If it is set to true, once a touch is captured, all lower-priority receiving objects cannot receive the touch. That is, when registering TargetTouchDelegate, you can set the bSwallowsTouches identifier. If a TargetTouchDelegate sets this identifier to true and needs to process a Touch event (ccTouchBegan

Returns true), The cocos2d-x will not send the Touch message to other TargetTouchDelegate and StandardTouchDelegate after being tuned to the Delegate.


For example, CCMenu is a touch receiver that will "Swallow" and has a priority of-128. Because of its high priority, menu buttons can always obtain a touch response.

 

 

Ii. touch event distribution sequence

The cocos2d-x first distributes events to CCTargetedTouchDelegate, and then sends events to CCStandardTouchDelegate. TouchDelegate of the same type is based on the registered priority.

To determine the distribution order. If the priorities are the same, events are distributed in the registration order.

 

 

 

 

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.