iphone - Cocos2d iOS : Why doesn't my CCMenuItemImage respond to touch events?

标签 iphone ios ipad cocos2d-iphone

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

接口(interface)文件

@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

提前非常感谢您的智慧!

最佳答案

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

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

关于iphone - Cocos2d iOS : Why doesn't my CCMenuItemImage respond to touch events?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283498/

相关文章:

iphone - 如何像FaceGoo一样进行图像处理

iphone - UIScrollView 偶尔会失去弹跳和流畅的滚动

iphone - iOS 在 UITableViewCell 中创建类似 iBook 的界面

ios - iOS 应用程序的自定义标签实现和使用

iPhone - 应用程序被弹出

ios - 如何调整 UIImageView 的大小(快速)

ios - 如何在 NSURL get 请求中传递查询项?

iphone - 如何在ipad上显示 "gallery"?

ipad - 如何在 iPad 上设置 NSAttributedString 的颜色?

iphone - 开发移动 C# 应用程序以在多个平台上运行