将CCMenu对齐屏幕顶部

时间:2012-10-11 18:47:29

标签: cocos2d-iphone alignment

我原本期望在屏幕左上角的“切换播放器”按钮上方有一个“菜单”按钮,但“菜单”按钮的上半部分位于屏幕上方。怎么会?这是一个错误吗?

左对齐没问题。

谢谢!

-(void) superSceneEnded
{
  _sceneEnded = true;

  CCMenu *menu = [CCMenu menuWithItems:nil];
  menu.anchorPoint = ccp(0, 1); // Upper left corner
  menu.position = ccp(2, _winSize.height - 2); // Almost at upp left corner of screen
  [self addChild:menu];

  [self addButton:menu :@"Menu" :@selector(goToMenu)];
  if ([MatchMgr instance].currentMatch.isOnDevice)
  {
    [self addButton:menu :@"Switch player" :@selector(switchPlayer)];
  }
  [menu alignItemsVerticallyWithPadding:10];
}

-(void) addButton: (CCMenu*) menu: (NSString*) text: (SEL) selector
{
  CCMenuItem *menuItem = [CCMenuItemImage itemFromNormalImage:@"button.png" selectedImage:@"button-pressed.png" target:self selector:selector];
  menuItem.anchorPoint = ccp(0, 1); // Upper left corner
  //menuItem.scale = 0.5;
  [menu addChild:menuItem];

  // The code below is not relevant for the question, but I keep it for your understanding.
  CCLabelBMFont *bmText = [CCLabelBMFont labelWithString:text fntFile:@"MainFont.fnt"];
  bmText.color = ccBLACK;
  bmText.scale = 0.75;
  bmText.alignment = NSTextAlignmentCenter;
  bmText.position = ccp(75, 15);
  [menuItem addChild: bmText];

}

1 个答案:

答案 0 :(得分:3)

这似乎真的被打破了: http://www.cocos2d-iphone.org/forum/topic/8261

我自己放置按钮。

这是我的,不那么花哨的代码:

-(void) superSceneEnded
{
  _sceneEnded = true;

  CGFloat yPos = _winSize.height - 10;
  CCMenu *menu = [CCMenu menuWithItems:nil];
  menu.position = ccp(0, 0);
  [self addChild:menu];

  [self addButton:menu :@"Menu" :@selector(goToMenu): &yPos];
  if ([MatchMgr instance].currentMatch.isOnDevice)
  {
    [self addButton:menu :@"Switch player" :@selector(switchPlayer): &yPos];
  }
}

-(void) addButton: (CCMenu*) menu: (NSString*) text: (SEL) selector: (CGFloat*) yPos
{
  CCMenuItem *menuItem = [CCMenuItemImage itemFromNormalImage:@"button.png" selectedImage:@"button-pressed.png" target:self selector:selector];
  menuItem.scale = 0.5;
  menuItem.position = ccp(42, *yPos);
  *yPos -= 20;
  [menu addChild:menuItem];

  CCLabelBMFont *bmText = [CCLabelBMFont labelWithString:text fntFile:@"MainFont.fnt"];
  bmText.color = ccBLACK;
  bmText.scale = 0.75;
  bmText.alignment = NSTextAlignmentCenter;
  bmText.position = ccp(75, 15);
  [menuItem addChild: bmText];
}