Search code examples
swiftsprite-kitvelocityskaction

Resetting and changing speed on SKActions


Is there a way to change the velocity/speed of a skaction and also to reset the skaction?

let wait = SKAction.waitForDuration(5.0)

let moveRight = SKAction.moveByX(300, y:0, duration: 1.0)

let sequence = SKAction.sequence([wait, moveRight])

let endlessAction = SKAction.repeatActionForever(sequence)

node.runAction(endlessAction)

This code works but the things i would like to change is how fast the SKSpriteNode moves to the right as at the moment it it quite slow and also to make the SKSpriteNode return to its orignal position rather than keep on moving to right forever?

Thank you


Solution

  • Since velocity = distance / time, decreasing the duration will increase the speed the the sprite will move across the screen.

    Regarding your second point, by considering how SKAction.moveByX(300, y:0, duration: 1.0) moves the node to the right; SKAction.moveByX(-300, y:0, duration: 1.0) must therefore move the node to the left, back to its original position.

    Hope that helps.