Details about Cocos2d development Touch events in CCLayer

Source: Internet
Author: User

Cocos2d DevelopmentAboutCCLayerMediumTouch eventIs the content to be introduced in this article, mainly to learnCocos2d DevelopmentMediumTouch event.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 and cocos2d official website.

Cocos2d development provides two touch processing methods. For the Standard Touch Delegate and Targeted Touch Delegate methods, see CCTouchDelegateProtocol. source code in h). By default, CCLayer adopts the first method. For more information, see the registerWithTouchDispatcher method of CCLayer ).

InCCLayerTo receive touch events in the 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; 

WhenTouch eventThis 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. -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;   
  3. -(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 it before using targeted.CCLayerThe registerWithTouchDispatcher method in:

 
 
  1. // Remember to import "CCTouchDispatcher. h" in the header file"
  2.  
  3. -(Void) registerWithTouchDispatcher {
  4. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate: self priority: 0 swallowsTouches: YES];
  5. }

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 DevelopmentAboutCCLayerMediumTouch eventI hope this article will help you.

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.