我希望克隆 Candy Crush Saga 中的按钮动画效果。我也想知道如何制作这样流畅而美丽的动画。Cocos2d-iPhone 可以吗?
这是 Candy Crush Sage 的链接:
http://www.youtube.com/watch?v=KAMUWIqYN24
它是使用图像序列完成的吗?
我希望克隆 Candy Crush Saga 中的按钮动画效果。我也想知道如何制作这样流畅而美丽的动画。Cocos2d-iPhone 可以吗?
这是 Candy Crush Sage 的链接:
http://www.youtube.com/watch?v=KAMUWIqYN24
它是使用图像序列完成的吗?
有可能的。只需在按钮正常精灵上运行动画即可。
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
这就是我使用 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]];
}}];
在我的情况下,我只在按下按钮时运行动画。