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.