xcode 如何让 cocos2d 精灵每秒向上和向下缩放(以获得脉动效果)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3185186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:31:03  来源:igfitidea点击:

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

iphonexcodeopengl-escocos2d-iphone

提问by RexOnRoids

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?

我想每秒向上和向下缩放一次精灵,以使其看起来像是膨胀和脉动。我怎样才能做到这一点?

回答by ZenQW

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

回答by hhamm

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 :

您可以使用简单的 [CCScaleTo ..] 操作,或者如果您想创建自己的“效果”,您可以推进 CCFiniteTimeAction。我更喜欢第一个:

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

使用 CCEase 使动画非线性

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.

这篇文章可能包含错误。但我希望你了解达到目标的方法。