1

我是 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)]];

但是当我点击它时什么也没发生。问题出在哪里?如何正确执行?

4

1 回答 1

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];
于 2013-01-02T22:26:08.530 回答