Cocos2d iOS:为什么我的CCMenuItemImage不响应触摸事件?

时间:2011-11-27 03:45:25

标签: iphone ios ipad cocos2d-iphone

我正在创建一个新类来表示场景中的标签,我正在试图弄清楚如何让它们响应触摸事件。我的代码非常简单,但是当我在initFromNormalImage方法中指定的选择器中设置断点时,断点永远不会被击中。这是我的代码......

接口文件

@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 个答案:

答案 0 :(得分:1)

您必须使用CCMenu类作为CCMenuItem和派生类的父级。您正在将CCMenuItemImage添加到self,这是一个CCNode实例。

相反,创建一个CCMenu类并将其添加到您的节点,然后将所有CCMenuItem *实例添加到CCMenu。请参阅this tutorial for more details