2011-11-27 75 views
0

我創建一個新的類在現場代表的標籤,我試圖找出如何讓他們響應觸摸事件。我的代碼是非常簡單的,但是當我設置在initFromNormalImage方法中的選擇斷點,斷點從來沒有被擊中。這裏是我的代碼...的Cocos2D的iOS:爲什麼我的CCMenuItemImage響應觸摸事件?

接口文件

@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 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 

感謝這麼多提前爲你的智慧!

回答

1

必須使用CCMenu類作爲父爲CCMenuItem和派生類。您正在將CCMenuItemImage添加到self,這是一個CCNode實例。

相反,創建一個類CCMenu並添加到您的節點,然後添加所有CCMenuItem *實例的CCMenu。請參閱this tutorial for more details