如何更改Cocos2d MenuItem的颜色?

Rob*_*yer 6 iphone objective-c cocos2d-iphone

[MenuItemFont setFontSize:20];
[MenuItemFont setFontName:@"Helvetica"];
//I'm trying to change the color of start (below item)
MenuItem *start = [MenuItemFont itemFromString:@"Start Game" 
                                        target:self 
                                      selector:@selector(startGame:)];
MenuItem *help = [MenuItemFont itemFromString:@"Help"
                                       target:self 
                                     selector:@selector(help:)];
Menu *startMenu = [Menu menuWithItems:start, help, nil];
[startMenu alignItemsVertically];
[self add:startMenu];
Run Code Online (Sandbox Code Playgroud)

小智 5

MenuItemFont *start =  [MenuItemFont itemFromString:@"Start Game" 
                                             target:self 
                                           selector:@selector(startGame:)];

[start.label setRGB:0 :0 :0]; // Black menu item
Run Code Online (Sandbox Code Playgroud)

Label是MenuItemFont的一个属性,是MenuItem的子类,因此在隐式转换为MenuItem时会丢失它.

或者,您可以这样做:

[((MenuItemFont *)start).label setRGB:0 :0 :0] 
Run Code Online (Sandbox Code Playgroud)

(但这很难看,而startMenu将采用一个没有投诉的MenuItemFont).

请记住,在MenuItemFont中,大部分颜色都是硬编码的,因此调用'setIsEnabled'会将颜色设置为灰色或白色.这发生在MenuItem.m的第239行附近,如果你需要调整它.如果我开始制作一个补丁来在MenuItemFont上公开这个功能(假设它还没有在.7.1之前的源代码中)我会更新我的帖子.