我正在尝试创建一个始终使用相同精灵的按钮,并且只要轻敲它就会向上缩放一点并再次向后缩小。这是我创建按钮的方式:
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];
按钮显示并正确调用选择器,但单击时不会展开。为了解决这个问题,我需要更改什么?
答案 0 :(得分:1)
button.zoomWhenHighlighted = YES;
根据@ LearnCocos2d的建议,属性BOOL zoomWhenHighlighted
可用于获取您想要的效果。您可能还想在实现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]];
}
答案 1 :(得分:0)
使用2张图片:
然后使用:
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];
保持编码...............:)