I’m trying to create a extended CCSprite class that can detect touches. I have done some research and discovered an example at http://anny.fm/c/iphone/cocos2d/TouchableSprite/ which was created by Anny in this forum thread http://www.cocos2d-iphone.org/forum/topic/3196 (last post).
Using this I have setup my class like so:
Header
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface PianoKey : CCSprite <CCTargetedTouchDelegate> {
}
-(BOOL) tsTouchBegan: (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchMoved: (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchEnded: (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event;
@end
and implementation…
@implementation PianoKey
-(id)initWithKey:(int)key {
if((self = [super initWithFile:[NSString stringWithFormat:@"key_%i.png",key]])) {
}
return self;
}
-(BOOL) tsTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event {
NSLog( @"tsTouchBegan");
return YES;
}
-(void) tsTouchMoved:(UITouch*)touch withEvent: (UIEvent*)event {
NSLog( @"tsTouchMoved");
}
-(void) tsTouchEnded:(UITouch*)touch withEvent: (UIEvent*)event {
NSLog( @"tsTouchEnded");
}
-(void) tsTouchCancelled:(UITouch*)touch withEvent: (UIEvent*)event {
NSLog( @"tsTouchCancelled");
}
//
// Utilities. Don't override these unless you really need to.
//
-(CGRect) rect {
CGSize s = [self.texture contentSize];
return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
-(BOOL) didTouch: (UITouch*)touch {
return CGRectContainsPoint( [self rect], [self convertTouchToNodeSpaceAR: touch] );
}
//
// The actual touch listener functions. Don't override these either.
//
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event {
NSLog(@"attempting touch.");
if([self didTouch: touch]) {
return [self tsTouchBegan:touch withEvent: event];
}
return NO;
}
-(void) ccTouchMoved: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch]) [self tsTouchMoved: touch withEvent: event]; }
-(void) ccTouchEnded: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch]) [self tsTouchEnded: touch withEvent: event]; }
-(void) ccTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch]) [self tsTouchCancelled: touch withEvent: event]; }
@end
I understand the methods above, such as detecting if the the touch was within the bounds of the sprite, however I’m stuck as to why I’m not getting any response when I click the key. I’m new to implementing CCTargetdTouchDelegate so I assume it might be something to do with that…
You don’t need the targeted touch delegate, it just splits the NSSet of touches into individual ccTouchXXX messages. Your implementation is simply lacking the registration and de-registration of the CCTargetedTouchHandler. This is usually done in onEnter, so that it works with any node type and not just CCLayer:
Btw, Kobold2D already has an extended CCNode class that can test if a touch was on a node (sprites, labels, etc) via this test:
Just saying you wouldn’t have had to do all this work. 😉