Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cocos2d sprite scale up and down (for a pulsating effect) every second?

I want to have a sprite scale up and down once every second to make it seem like it is bulging and pulsating. How can I do this?

like image 357
RexOnRoids Avatar asked Jul 06 '10 09:07

RexOnRoids


2 Answers

As the post before contains syntax errors. To be more precise, I post working code:

CCSprite * sprite = ...; // create the sprite.
id scaleUpAction =  [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:1 scaleX:1.0 scaleY:1.0] rate:2.0];
id scaleDownAction = [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.8 scaleY:0.8] rate:2.0];
CCSequence *scaleSeq = [CCSequence actions:scaleUpAction, scaleDownAction, nil];
[sprite runAction:[CCRepeatForever actionWithAction:scaleSeq]];
like image 51
ZenQW Avatar answered Oct 05 '22 23:10

ZenQW


You could use a simple [CCScaleTo ..] action or if you want to create your own "effect" you could advance the CCFiniteTimeAction. I would prefer the first one :

CCSprite * sprite = ...; // create the sprite.
sprite.anchorPoint = ccp( 0.5, 0.5 ); center the pivot
id myAction = [CCRepeatForEver actionWithActions:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0],[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5], nil];
[sprite runAction:myAction];

use CCEase to make the animation nonlinear

id myAction = [CCRepeatForEver actionWithActions:[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0] rate:2.0],[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5] rate:2.0], nil];

this post may contain errors. but I hope you understand the way to come to the goal.

like image 31
2 revs Avatar answered Oct 06 '22 01:10

2 revs