Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the color of a Cocos2d MenuItem?

[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];
like image 903
Rob Sawyer Avatar asked Feb 17 '09 19:02

Rob Sawyer


2 Answers

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.

like image 179
JustinB Avatar answered Nov 10 '22 00:11

JustinB


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

[start.label setColor: ccc3(200,0,200)];
like image 27
Chewie The Chorkie Avatar answered Nov 09 '22 23:11

Chewie The Chorkie