Guru
Guru

Reputation: 22042

CCMenuItemSprite's alternative in Cocos2d v3

In Cocos2d 2.0 I used below code to use single image for normal and selected image with colour change on selection.

CCSprite *twitter_1     = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_1];
CCSprite *twitter_2     = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_2];
twitter_2.color = ccc3(128,128,128);

CCMenuItemSprite *twitterBtn = [CCMenuItemSprite itemWithNormalSprite:twitter_1
                                                       selectedSprite:twitter_2
                                                               target:self
                                                             selector:@selector(twitterBtnPress:) ];

In Cocos2d v3, I can use CCButton as alternative, but how to change selected frame colour?

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];

CCButton * twitterBtn = [CCButton buttonWithTitle:@""
                                     spriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
                          highlightedSpriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
                             disabledSpriteFrame:nil];

twitterBtn = CCPositionTypeNormalized;
twitterBtn.position = ccp(0.5f, 0.5f);
[twitterBtn setTarget:self selector:@selector(playBtnPress:)];
[self addChild: twitterBtn];

Now in Cocos2d v3, how to use CCSprite for button and change colour?

Upvotes: 0

Views: 1872

Answers (1)

Ben-G
Ben-G

Reputation: 5026

You can use the method:

- (void) setBackgroundColor:(CCColor*)color forState:(CCControlState)state

of CCButton to set a different background color for the different states.

Upvotes: 1

Related Questions