1

所以我希望我的主菜单有一个 ccMenu。我尝试使用垂直对齐和 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];

游戏画面

好像什么都没做。代码有什么问题?

4

3 回答 3

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

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

于 2012-05-06T00:53:39.957 回答
4

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

CCMenuItem * testItem = ...
testItem.anchorPoint = ccp(0, 0.5f);
[testItem alignItemsVertically]
于 2012-08-10T03:40:45.290 回答
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)];

像这样使用它...希望这会有所帮助.. :)

于 2012-05-06T06:22:25.903 回答