Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

how to make an animation an infinite loop for 2d games in sprite kit?

Can anyone give me some advice on how to code to make a ground node loop infinitely for a spriteKit game? Videos online don't really seem to help. I"m coding in Swift if thats any help, but i understand abit on objective C. thanks in advance

1 Answer

Stone Preston
Stone Preston
42,016 Points

for an action named someAction, you can make that action repeat forever by calling the repeatActionForever class method of SKAction and passing in the action you want to repeat as an argument

//this rotates the sprite over 1 second
let someAction = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

//this will rotate the sprite indefinitely
let someActionRepeatingForever = SKAction.repeatActionForever(someAction)

then you would just need to run the someActionRepeatingForever action on your ground sprite node

someGroundSprite.runAction(someActionRepeatingForever)