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

妖精的绣舞 提交于 2019-12-07 01:40:46

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3185186/how-to-make-cocos2d-sprite-scale-up-and-down-for-a-pulsating-effect-every-seco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!