在Cocos2D V3中单击时展开的Sprite按钮

时间:2014-06-15 14:43:32

标签: ios objective-c button cocos2d-iphone

我正在尝试创建一个始终使用相同精灵的按钮,并且只要轻敲它就会向上缩放一点并再次向后缩小。这是我创建按钮的方式:

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];

按钮显示并正确调用选择器,但单击时不会展开。为了解决这个问题,我需要更改什么?

2 个答案:

答案 0 :(得分:1)

button.zoomWhenHighlighted = YES;

根据@ LearnCocos2d的建议,属性BOOL zoomWhenHighlighted可用于获取您想要的效果。您可能还想在实现CCButton.m

中使用scaleTo值
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]];
            }

答案 1 :(得分:0)

使用2张图片:

  1. 一个正常
  2. 下一步按(上面相同的画布尺寸,但图像缩放到< 100%)
  3. 然后使用:

    CCSpriteFrame *backButtonSprite = [CCSpriteFrame frameWithImageNamed:@"backButton.png"];
    CCSpriteFrame *backButtonPressedSprite = [CCSpriteFrame
                                              frameWithImageNamed:@"backButtonPressed.png"];
    
    CCButton *myButton = [CCButton buttonWithTitle:@""
                                        priteFrame:backButtonSprite
                            highlightedSpriteFrame:backButtonPressedSprite
                               disabledSpriteFrame:nil];
    
    [myButton setTarget:self selector:@selector(myFunction:)]; 
    myButton = ccp(100,100);
    [self addChild:backButton];
    

    enter image description here

    保持编码...............:)