Thalatta Thalatta - 2 months ago 17
Swift Question

Swift: change speed of a moveTo SKAction

I have a simple sprite movement that I am triggering with the following line:

actionMove = SKAction.moveTo(CGPoint(x: size.width + CGFloat(KUGELWIDTH / 2), y: yPos), duration: NSTimeInterval(actualDuration))


And then i call
runAction
in the following manner:

let actionMoveDone = SKAction.removeFromParent()
kugel.runAction(SKAction.sequence([actionMove, actionMoveDone]))


What i am confused about, is changing the speed (duration) of the
SKAction
in the middle of while it is in the middle of running. This may be impossible, in which case, is there another method to invoke to move a sprite around my
Scene
with which I can change the Sprite's speed in the middle of its movement?

Answer

You can't change the speed of an action itself, because SKAction objects are "fire and forget" and reusable — once you pass one to a node's runAction method, what you do to that action instance no longer affects what the node is doing to run the action.

But there are a few other options here.

  1. Change the speed property of the node being animated. You can do this either directly or by running a speedBy or speedTo action. This property is a multiplier that changes how fast that node runs any and all actions attached to it, so you can set it below 1.0 to slow down whatever's going on, or above 1.0 to speed things up.

  2. Control the node more directly with a custom action that periodically runs a block; in that block, you can directly set the node's position, and you can check external state to determine whether to move the mode more or less per time interval.

  3. Control the node's position directly during your scene's (or scene delegate's) update loop, at which point you can control the amount of movement per frame however you like.