David Lawson
David Lawson

Reputation: 7922

Cocos2D CCButton setBackgroundColor not working

I added the following code to the Cocos2D default template's IntroScene:

CCButton *playButton = [CCButton buttonWithTitle:@"Play" fontName:@"HelveticaNeue" fontSize:18.0f];
[playButton setBackgroundColor:[CCColor colorWithWhite:0.7f alpha:1.0f] forState:CCControlStateNormal];
[playButton setBackgroundColor:[CCColor colorWithWhite:0.75f alpha:1.0f] forState:CCControlStateHighlighted];
playButton.positionType = CCPositionTypeNormalized;
playButton.position = ccp(0.5f, 0.35f);
playButton.zoomWhenHighlighted = NO;
playButton.preferredSize = CGSizeMake(222, 46);
[playButton setTarget:self selector:@selector(onSpinningClicked:)];
[self addChild:playButton];

For some reason the button has no background color. Any ideas?

Upvotes: 3

Views: 1576

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

Because you didn't set a color, only brightness (this is what "white" means in the context of CCColor).

Try using a color with this initializer:

CCColor* normalColor = [CCColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0f]
[playButton setBackgroundColor:normalColor forState:CCControlStateNormal];

This will make the button background red.

Upvotes: 2

Related Questions