Two touch processing methods in Cocos2D-iphone cclayer

Source: Internet
Author: User

Cocos2d provides two touch Processing Methods: standardtouch delegate and targetedtouch delegate.

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 delegate(Cclayer adopts this method by default) ----- This is a common method.
To handle touch events in this way, you can choose to implement these four methods.
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    }-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    }-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    }-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    }

According to the name, we can know that the two stages are processing the touch. The cancel is rarely used, and the end is generally used. The following is an example:

// Handle touch events-(void) cctouchesbegan :( nsset *) touches withevent :( uievent *) event {for (uitouch * touch in touches) {cgpoint location = [Touch locationinview: [Touch view]; location = [[ccdirector shareddire] converttogl: location]; nslog (@ "x = % F, y = % F", location. x, location. Y );}}

Click the screen to output the GL coordinate system coordinates of the clicked point.

  • Targetedtouch delegate----- This method is rarely used
The use of this method is a long term for many blogs on the Internet. It is no longer applicable to the current version. The following describes how to use the latest V2.1 version. First, add the proxy in the init method.
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

Then implement the following methods. The first method must be implemented.

-(Bool) cctouchbegan :( uitouch *) touch withevent :( uievent *) event; // (must be implemented)-(void) cctouchmoved :( uitouch *) touch withevent :( uievent *) event;-(void) cctouchended :( uitouch *) touch withevent :( uievent *) event;-(void) cctouchcancelled :( uitouch *) touch withevent :( uievent *) event;

Finally, remove the proxy.

-(void)onExit {    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];    [super onExit];}

The following example shows how to use it:

// On "init" you need to initialize your instance-(ID) init {// always call "super" init // Apple recommends to re-assign "self" with the "super's" Return valueif (Self = [Super initwithcolor: ccc4 (255,255,255,255)]) {[self settouchenabled: Yes]; [[ccdirector shareddirector] touchdispatcher] addtargeteddelegate: Self priority: 0 swallowstouches: Yes];} return self ;} -(bool) cctouchbegan :( uitouch *) touc H withevent :( uievent *) event {cgpoint touchpoint = [self converttouchtonodespace: Touch]; cclog (@ "x = % F, y = % F", touchpoint. x, touchpoint. y); // If yes is returned, the touch event will be passed. If no is returned, the event will not be passed. Return no;}-(void) onexit {[[ccdirector shareddire] touchdispatcher] removedelegate: Self]; [Super onexit];}

The two code examples listed above have the same effect.

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.