CCMenuItemSprite's alternative in Cocos2d v3

烂漫一生 提交于 2019-12-13 04:43:11

问题


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?


回答1:


You can use the method:

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

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



来源:https://stackoverflow.com/questions/22023663/ccmenuitemsprites-alternative-in-cocos2d-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!