如何在Cocos2d中处理CCMenuItem

时间:2013-01-02 21:59:39

标签: objective-c cocos2d-iphone ccmenuitem

我是Cocos2d的新手,尝试从正常的Cocoa Touch到Cocos2d的端口游戏。

我通过自定义方法创建了一些按钮:

+ (CCMenuItem *)createMenuItemImageWithNormalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage tag:(int)tag target:(id)target selector:(SEL)selector position:(CGPoint)position {

    CCMenuItem *item = [CCMenuItemImage itemWithNormalImage:normalImage selectedImage:selectedImage];
    item.tag = tag;
    [item setTarget:target selector:selector];
    item.position = [TSCCTransform transformPositionFromCocoaTouchToCocos2d:CGPointMake(position.x, position.y)];

    return item;
}

按钮:

    [self addChild:[TSCCLayer createMenuItemImageWithNormalImage:@"btn-mouse-dec.png" selectedImage:@"btn-mouse-dec.png" tag:0 target:self selector:@selector(changeNumberOfPlayers:) position:ccp([TSCCTransform winSizeWidth] - 140, 146)]];

但是当我点击它时没有任何反应。哪里有问题?怎么做得对?

1 个答案:

答案 0 :(得分:1)

您不必向其添加菜单项,而是添加菜单。

将CCMenuItem添加到CCMenu,然后将菜单添加为图层的子级:

CCMenuItem* item=[TSCCLayer createMenuItemImageWithNormalImage:@"btn-mouse-dec.png" selectedImage:@"btn-mouse-dec.png" tag:0 target:self selector:@selector(changeNumberOfPlayers:) position:ccp([TSCCTransform winSizeWidth] - 140, 146)]
CCMenu* menu= [CCMenu menuWithItems: item, nil];  // item becomes child of menu
[self addChild: menu];