How to create auto-updating animation that will forever loop in the Sprite Kit?

I am trying to make a simple auto-changing animation.

SKAction *a = [SKAction moveToX:10 duration:0.5];
a = [SKAction repeatActionForever:a];
[car runAction:a];

But the action is not canceled. How do you get a similar auto-reverse effect, for example, using Core Animation?

+4
source share
3 answers

The answer from Andrey Gordeev is close enough,

float x = car.position.x;
SKAction *a = [SKAction moveToX:(x+10) duration:0.5];
SKAction *b = [SKAction moveToX:x duration:0.5];
SKAction *seq = [SKAction sequence:@[a,b]];
[car runAction:[SKAction repeatActionForever:seq]];

moveToX: (x + 10) will make it swing by 10 (instead of going to point X = 10).

+9
source

The repeatActionForever : method cancels the action.

The problem is moveToX : an action that is not reversible

Apple , .

PS: moveByX: y: duration:

+1

Add the reverse action manually:

float x = car.position.x;
SKAction *a = [SKAction moveToX:10 duration:0.5];
SKAction *b = [SKAction moveToX:x duration:0.5];
SKAction *sequence = [SKAction sequence:@[a,b]];
[car runAction:[SKAction repeatActionForever:sequence]];
+1
source

All Articles