存档

2009年2月 的存档

How do I change the color of a Cocos2d MenuItem?

2009年2月17日 没有评论
[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];
MenuItemFont *start =  [MenuItemFont itemFromString:@"Start Game" 
                                             target:self 
                                           selector:@selector(startGame:)];

[start.label setRGB:0 :0 :0]; // Black menu item

Label is a property of MenuItemFont, a subclass of MenuItem, so you lose it during the implicit cast to MenuItem.

Alternatively, you could do:

[((MenuItemFont *)start).label setRGB:0 :0 :0]

(but that’s ugly, and startMenu will take a MenuItemFont with no complaints).

Keep in mind that the colors are for the most part hardcoded in MenuItemFont, so calling ‘setIsEnabled’ will set the colors back to grey or white. This happens around line 239 of MenuItem.m if you need to tweak it. If I get around to making a patch to expose this functionality on MenuItemFont (assuming it’s not already in the pre-.7.1 sources) I’ll update my post.

You can change it like this (at least on Cocos2d version 0.99.5)

CCMenuItemFont *startMenuItem = [CCMenuItemFont itemFromString:@"Start" target:self selector:@selector(startTapped:)];
[startMenuItem setColor:ccBLACK];

setRGB has been set to setColor in newer versions. For example:

[start.label setColor: ccc3(200,0,200)];
分类: stackoverflow精选, unity3d 标签: