0

我创建了一个新层,向它们添加了三个菜单项,并且我对它们中的每一个都有方法,它们登录到控制台并切换视图,但没有任何反应。为什么会这样?

- (id) init
{
  if (self = [super init])
  {  
    [self setIsTouchEnabled:YES];
    CCSprite *background = [CCSprite spriteWithFile:@"bg.png"];
    [background setPosition:CGPointMake(size.width / 2, size.height / 2)];
    [self addChild:background z: 0];
    CCMenuItemFont *menu1 = [CCMenuItemFont itemFromString:@"New game" target:self selector:@selector(startNewGame)];
    [menu1 setPosition:CGPointMake(size.width / 2, 330)];
    CCMenuItemFont *menu2 = [CCMenuItemFont itemFromString:@"Options" target:self selector:@selector(options)];
    [menu2 setPosition:CGPointMake(size.width / 2, 250)];
    CCMenuItemFont *menu3 = [CCMenuItemFont itemFromString:@"Credits" target:self selector:@selector(credits)];
    [menu3 setPosition:CGPointMake(size.width / 2, 170)];
    [self addChild:menu1];
    [self addChild:menu2];
    [self addChild:menu3];
  }
  return self;
}

- (void) credits
{
  NSLog(@"In credits");
}

// same methods for new game and options. It never gets called.
4

1 回答 1

1

CCMenuItem 实例必须添加到 CCMenu 实例中,该实例必须添加到图层中。

CCMenu *mainMenu = [CCMenu menuWithItems:menu1, menu2, menu3, nil];
mainMenu.position = CGPointZero;
[self addChild:mainMenu];

祝你好运!

于 2012-07-26T18:34:06.410 回答