How to change the color of 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]; 
+6
objective-c iphone cocos2d-iphone
source share
3 answers
 MenuItemFont *start = [MenuItemFont itemFromString:@"Start Game" target:self selector:@selector(startGame:)]; [start.label setRGB:0 :0 :0]; // Black menu item 

A shortcut is a property of MenuItemFont, a subclass of MenuItem, so you lose it during implicit casting to MenuItem.

Alternatively you can do:

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

(but it's ugly, and startMenu will take MenuItemFont without any complaints).

Keep in mind that in most cases colors are hardcoded in MenuItemFont, so calling setIsEnabled sets the colors back to gray or white. This happens around line 239 of MenuItem.m if you need to configure it. If I manage to make a patch to expose this function in MenuItemFont (assuming that this is not in pre-.7.1 sources yet), I will update the message.

+5
source share

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

 [start.label setColor: ccc3(200,0,200)]; 
+4
source share

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]; 
+3
source share

All Articles