在cocos2d中实现动画按钮

时间:2013-05-07 12:42:23

标签: cocos2d-iphone box2d-iphone

我想克隆Candy Crush Saga中发现的按钮动画的效果。 而且我也想知道如何做这样的液体&美丽的动画。 用Cocos2d-iPhone可以吗?

这是Candy Crush Sage的链接:

http://www.youtube.com/watch?v=KAMUWIqYN24

是否使用图像序列完成?

2 个答案:

答案 0 :(得分:6)

有可能。只需在按钮普通精灵上运行动画。

GTAnimSprite *frame_normal   = [GTAnimSprite spriteWithFile:@"play_button_normal.png"];
GTAnimSprite *frame_sel     = [GTAnimSprite spriteWithFile:@"play_button_selected.png"];
frame_sel.color = ccc3(128,128,128);

CCMenuItemSprite *plyBtn = [CCMenuItemSprite itemWithNormalSprite:frame_normal
                                          selectedSprite:frame_sel
                                                  target:self
                                                selector:@selector(playBtnPress:) ];

plyBtn.position = ccp(size.width*0.77f, size.height*0.1f);

CCMenu *menu2 = [CCMenu menuWithItems:plyBtn, nil];
menu2.position = ccp(0.0f,0.0f);
[self addChild:menu2 z:2 ];

//这是类文件:GTAnimSprite.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface GTAnimSprite : CCSprite
{
    bool bouncing;
    float counter;

}
@end

//这是类文件:GTAnimSprite.mm

#import "GTAnimSprite.h"

@implementation GTAnimSprite

-(void)onEnter
{
    [super onEnter];

    counter = 0.0f;

    bouncing = true;

    [self scheduleUpdate];
}

-(void)update:(ccTime)dt
{
    if (bouncing)
    {
        counter += dt;

        self.scaleX = ( (sin(counter*10) + 1)/2.0 * 0.1 + 1);
        self.scaleY = ( (cos(counter*10) + 1)/2.0 * 0.1 + 1);

        if (counter > M_PI*10){
            counter = 0;
        }
    }
}

-(void)onExit
{
    [self unscheduleUpdate];

    [super onExit];
}

@end

这里是XCODE样本来源:https://www.box.com/s/52i4xyznetyyc329evcu

答案 1 :(得分:3)

这就是我使用CCActions的方式

首先,我定义了CCAction:

 id scaleHorDown = [CCScaleTo actionWithDuration:duration * 5/30.f scaleX:0.75f scaleY:1.0f];
 id scaleHorBouncing = [CCEaseBounceIn actionWithAction:scaleHorDown];
 id scaleVerDown = [CCScaleTo actionWithDuration:duration * 5/30.f scaleX:1.0f scaleY:0.65f];
 id scaleVerBouncing = [CCEaseBounceInOut actionWithAction:scaleVerDown];

 id shrink = [CCSequence actions:scaleHorBouncing,scaleVerBouncing, nil];

 id swell = [CCScaleTo actionWithDuration: duration * 15/30.f scale:1.10f];
 id swellEase = [CCEaseElasticOut actionWithAction:swell];

 id resetScale = [CCScaleTo actionWithDuration:duration * 5/30.f  scale:1.0f];
 id resetScaleBouncing = [CCEaseInOut actionWithAction:resetScale];

 id buttonAction = [CCSequence actions: shrink, swellEase, resetScaleBouncing, nil];

然后,当初始化CCMenuItem时,我在想要的精灵上运行动画

CCMenuItem aMenuItem = [CCMenuItemSprite itemFromNormalSprite:buttonNormalSprite
                         selectedSprite:buttonSelectedSprite
                         block:^(id sender) {
                            //play animation highlighting button
                            [buttonSelectedSprite runAction:buttonAction]];
                                                        }}];

在我的情况下,我只是在按下按钮时运行动画。