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?
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.
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.
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.
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.
Using Physics instead of Actions:
Set a speed to a destination.
Create a joint at your destination, connecting your object to the destination, with damping and frequency of response. You can set this on arrival, or initially. Set it initially, with a strong enough frequency and you won't need to set the initial speed, it'll be "attracted" to the destination strongly enough to do all the animation for you.
At any point in the journey, you can adjust the frequency and impart other forces on the moving object to slow or speed it.