2012-10-11 88 views
1

我期望在屏幕左上角的「切換播放器」按鈕上方有一個「菜單」按鈕,但「菜單」按鈕的上半部分位於屏幕上方。怎麼來的?這是一個錯誤?將屏幕頂部的CCMenu對齊

左對齊是好的。

謝謝!

-(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]; 

}

+0

不要更改CCMenu的anchorPoint,它完全與觸摸檢測和定位食堂。只設置位置。 – LearnCocos2D

+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]; 
}