Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an autoreversing animation that loops forever in Sprite Kit?

I'm trying to do a simple automatically reversing animation.

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

But the action doesn't reverse. How do you get a similar autoreverse effect like with Core Animation?

like image 414
openfrog Avatar asked Dec 15 '22 03:12

openfrog


1 Answers

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 go at point X=10).

like image 179
GeneCode Avatar answered Dec 27 '22 10:12

GeneCode