Search code examples
iosobjective-cbuttoncocos2d-iphone

Sprite buttons that expand when clicked in Cocos2D V3


I am trying to create a button that always uses the same sprite, and which scales up a little and back down again whenever it is tapped. Here's how I create the button:

CCSpriteFrame *sprite = [CCSpriteFrame frameWithImageNamed:@"my-sprite.png"];

CCButton *button = [CCButton buttonWithTitle:@""
                                 spriteFrame:sprite
                      highlightedSpriteFrame:sprite
                         disabledSpriteFrame:nil];

[button setTarget:self selector:@selector(onButtonPressed:)];

[self addChild:button];

The button shows up and properly calls the selector, but it does not expand when clicked. What do I need to change in order to fix this?


Solution

  • button.zoomWhenHighlighted = YES;
    

    As suggested by @LearnCocos2d, the property BOOL zoomWhenHighlighted could be used to get the affect that you want. You may also want to play around with the scaleTo values in implementation CCButton.m

    if (_zoomWhenHighlighted)
                {
                    [_label runAction:[CCActionScaleTo actionWithDuration:0.1 scaleX:_originalScaleX*1.2 scaleY:_originalScaleY*1.2]];
                    [_background runAction:[CCActionScaleTo actionWithDuration:0.1 scaleX:_originalScaleX*1.2 scaleY:_originalScaleY*1.2]];
                }