How to use Touch in Cocos2d CCLayer

Source: Internet
Author: User

Cocos2d CCLayerMediumTouchThe usage is described in this article,Cocos2dAs an open-source 2D game engine, it was initially implemented in python. After mac app development became popular, it provided an Objective-C version. Using the Cocos2d framework to develop iphone Games greatly improves the development speed. For a brief introduction, see Baidu encyclopedia,Cocos2dOfficial website.

Cocos2dProvides two typesTouchProcessing method, StandardTouchDelegate and TargetedTouchFor the Delegate method, see the source code in CCTouchDelegateProtocol. h ),CCLayerThe first method is used by default. For more information, seeCCLayerThe registerWithTouchDispatcher method ).

To receive touch events in the CCLayer subclass, you must first activate touch support and set the value of isTouchEnabled to YES in the init method.

Standard Touch DelegateCCLayer adopts this method by default)

In the Standard method, you need to reload four basic touch processing methods:

 
 
  1. -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 

When a touch event occurs, this method is called to respond to the touch event. For a single touch, you only need to call UITouch * touch = [touches anyObject] to obtain the touch object. If you need to respond to multiple touch points, you need to call [event allTouches] allObjects] to return a UITouch NSArray object, and then use the objectAtIndex of NSArray to access each UITouch object in sequence. To obtain the coordinates of the UITouch object and assume that the UITouch name is touch), call [touch locationInView: [touch view] to return a coordinate viewPoint related to the UIView.

When you use the Cocos2d New application Wizard to create a new cocos2d application, CCDirector converts the UIView to an EAGLView that supports OpenGL ES in the applicationDidFinishLaunching method of the xxxAppDelegate class. In this case, we also need to convert the viewPoint in the previously obtained UIView to the EAGLView coordinate, and call [CCDirector shareddire] convertToGL: viewPoint] to achieve this.

 
 
  1. -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;  
  2.  
  3. -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;   
  4.  
  5. -(void) ccTouchesCancelled:(NSSet*)touch withEvent:(UIEvent *)event;  

These three methods are similar to ccTouchesBegan.

Targeted Touch Delegate Mode

In the standard mode, all the Response Processing events are handled by NSSet, while the targeted method only processes a single UITouch object. In the multi-point touch condition, the standard method should be adopted. You need to override the registerWithTouchDispatcher method in CCLayer before using the targeted method:

// Remember to import "CCTouchDispatcher. h" in the header file"

 
 
  1. -(void) registerWithTouchDispatcher {   
  2.          [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];  
  3.    } 

In the targeted mode, you need to reload four basic processing methods, of which ccTouchBegan must be rewritten, and the other three are optional.

 
 
  1. -(BOOL) ccTouchBegan :( UITouch *) touch withEvent :( UIEvent *) event; required)
  2. -(Void) ccTouchMoved :( UITouch *) touch withEvent :( UIEvent *) event;
  3. -(Void) ccTouchEnded :( UITouch *) touch withEvent :( UIEvent *) event;
  4. -(Void) ccTouchCancelled :( UITouch *) touch withEvent :( UIEvent *) event;

When a touch event occurs, call the ccTouchBegan method to respond to each UITouch and return a BOOL value. If YES, subsequent ccTouchMoved, ccTouchEnabled, and ccTouchCancelled will respond.

Multi-touch support

Add the following code to the applicationDidFinishLaunching method of the xxxAppDelegate class:

 
 
  1. [glView setMultipleTouchEnabled:YES]; 

Summary: DetailsCocos2d CCLayerMediumTouchI hope this article will help you with the introduction of the usage method!

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.