如何获得左对齐的cocos2d菜单

时间:2012-05-05 23:40:12

标签: objective-c ios xcode cocos2d-iphone

所以我希望我的主菜单有一个ccMenu。我尝试使用vertical vertical和alignItemsInColumns进行探索,但所有这些选项都将菜单中的项目居中。如果我想要这样的东西:

标题

 Option 1 has a very long name
 Option 2 is short
 Shorter

而不是:

标题

Option 1 has a very long name
     Option 2 is short
          Shorter

我怎样才能在cocos2d中实现这一目标?谢谢你的帮助。

修改

我尝试使用此代码,但我得到了这个结果:

CCLabelTTF * sin = [CCLabelTTF labelWithString:@"Single Player" dimensions:CGSizeMake(120,    25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item1 = [CCMenuItemLabel itemWithLabel:sin target:self selector:@selector(goToSinglePlayer:)];

CCLabelTTF * spl = [CCLabelTTF labelWithString:@"Splitscreen" dimensions:CGSizeMake(100, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item2 = [CCMenuItemLabel itemWithLabel:spl target:self selector:@selector(goToSplitscreen:)];

CCLabelTTF * ach = [CCLabelTTF labelWithString:@"Achievements" dimensions:CGSizeMake(130, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item3 = [CCMenuItemLabel itemWithLabel:ach target:self selector:@selector(goToAchievements:)];

CCLabelTTF * str = [CCLabelTTF labelWithString:@"Store" dimensions:CGSizeMake(50, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item4 = [CCMenuItemLabel itemWithLabel:str target:self selector:@selector(goToStore:)];

CCLabelTTF * set = [CCLabelTTF labelWithString:@"Settings" dimensions:CGSizeMake(80, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item5 = [CCMenuItemLabel itemWithLabel:set target:self selector:@selector(goToSettings:)];

CCMenu * mainMenu = [CCMenu menuWithItems:item1, item2, item3, item4, item5, nil];

[mainMenu setColor:ccBLACK];
[mainMenu alignItemsVerticallyWithPadding:10];
mainMenu.position =  ccp(size.width/6, size.height/2);
[self addChild:mainMenu];

Game Screen

似乎没有做任何事情。代码有什么问题?

3 个答案:

答案 0 :(得分:4)

使用CCMenuItemLabel项目创建菜单。使用CCLabelTTF创建每个标签,使其保持左对齐和已知尺寸。将menuItems添加到菜单时,如果所有菜单都具有相同的尺寸,它们将对齐。

补充道:

 CGSize label = CGSizeMake(160., 45.); 
 CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"Option1 for example" 
                                       dimensions:size 
                                        alignment:UITextAlignmentLeft
                                         fontName:@"pickYourOwnFont" fontSize:16.];

 CCLabelTTF *label2 = [CCLabelTTF labelWithString:@"Option2 below option 1" 
                                       dimensions:size 
                                        alignment:UITextAlignmentLeft 
                                         fontName:@"pickYourOwnFont" fontSize:16.];
CCMenuItem * item1 = [CCMenuItemLabel itemWithLabel:label1];
CCMenuItem * item2 = [CCMenuItemLabel itemWithLabel:label2];
CCMenu * leftAlignedMenu=[CCMenu menuWithItems:item1,item2,nil];

你可以为标签(颜色等等)和menuItem(回调目标和选择器)设置你喜欢的任何属性。

答案 1 :(得分:4)

更通用的解决方案(不需要使用CCLabel)只需调整锚点,然后垂直对齐即可。

CCMenuItem * testItem = ...
testItem.anchorPoint = ccp(0, 0.5f);
[testItem alignItemsVertically]

答案 2 :(得分:2)

CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World"
                                     dimensions:CGSizeMake(480, 100)
                                      alignment:UITextAlignmentLeft
                                       fontName:@"Marker Felt"
                                       fontSize:64];

CCMenuItemLabel *labelItem = [CCMenuItemLabel itemWithLabel:label
                                                     target:self
                                                   selector:@selector(labelPressed)];

像这样使用...希望这有助于.. :))