Search code examples
swiftsprite-kitskspritenodeskaction

how to move sprite node smoothly


I am currently moving a rocket using this code:

let location = touch.locationInNode(self)
rocket.position.y = location.y

I want to know if it would be easier to use the SKActionTimingMode.EaseOut and if so, how do I make the rocket move up and down along the Y-axis using an SKAction.

Is there a way to smoothly move a node with the code I am already using?

The app is set to landscape.


Solution

  • Here's how you can do it with SKAction. I think using SKAction would be much smoother. Alternatively, you could also attach a physics body to the node and call applyForce to it by calculating the force of the swipe or if its a touch then calling applyImpulse with a predefined value depending on where the touch occurred (i.e. above or below the object.)

    let location = touch.locationInNode(self)
    let action = SKAction.moveToY(location.y, duration: 3.0) // Or however much time you want to the action to run.
    action.timingMode = .EaseInEaseOut
    rocket.runAction(action)