I’m creating a new class to represent labels in the scene and I’m trying to figure out how to make them respond to touch events. My code is very simple, but when I set a breakpoint in the selector specified in the initFromNormalImage method, the breakpoint never gets hit. Here’s my code…
interface file
@interface FBLabel : CCNode {
CCMenuItemImage *_labelSprite;
CCLabelBMFont *_info;
}
-(id) initWithLabelFileName: (NSString*) filename andInfo: (NSString*) inInfo;
@property (nonatomic, retain) CCMenuItemImage *labelSprite;
@property (nonatomic, retain) CCLabelBMFont *info;
@end
implementation file
@implementation FBLabel
@synthesize labelSprite = _labelSprite;
@synthesize info = _info;
-(id) initWithLabelFileName: (NSString*) filename andInfo: (NSString*) inInfo{
self = [super init];
if(self){
_labelSprite = [CCMenuItemImage itemFromNormalImage:filename selectedImage:filename target:self selector:@selector(checkSelectedItem:)];
if(_labelSprite){
[self addChild: _labelSprite z:1];
self.contentSize = _labelSprite.contentSize;
}
//_info = [CCLabelBMFont labelWithString:inInfo fntFile:@"AppleLiGothic_Black18.fnt"];
_info = [CCLabelTTF labelWithString:inInfo fontName:@"Arial" fontSize:16.0];
if(_info){
_info.anchorPoint = ccp(0,0.5);
_info.position = ccp(-80,0);
[self addChild:_info z:2];
}
}
return self;
}
-(void) dealloc{
[_labelSprite release];
[_info release];
[super dealloc];
}
- (void)checkSelectedItem:(id)sender {
//THIS LINE OF CODE NEVER GETS EXECUTED
CCMenuItemImage *toggleItem = (CCMenuItemImage *)sender;
}
@end
Thanks so much in advance for your wisdom!
You must use the CCMenu class as the parent for CCMenuItem and derived classes. You are adding the CCMenuItemImage to self, which is a CCNode instance.
Instead, create a CCMenu class and add this to your node, and then add all CCMenuItem* instances to the CCMenu. Refer to this tutorial for more details.