Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping an running SKAction - Sprite Kit

The following code will animate a rotation.

let something:SKSpriteNode = SKSpriteNode()

func start(){
  let rotateAction = SKAction.rotateToAngle(CGFloat(M_PI), duration: 10.0)
  something.runAction(SKAction.sequence([rotateAction]))
}

Now I want to stop the animation within the animating duration. Is there anything similar to the following? I want to stop the animation when the user touches the screen.

func stop(){
  something.SKAction.stop()
}
like image 385
Clinton Lam Avatar asked Feb 19 '16 09:02

Clinton Lam


2 Answers

You just have to use either:

  1. something.paused = false // or true to pause actions on the node
  2. something.removeAllActions() to definitely remove actions associated to the node
  3. name your action when launching something.runAction(action,withKey:"action1") and then something.removeActionForKey("action1") to remove a given action

You may also change the speed if needed.

like image 194
Jean-Baptiste Yunès Avatar answered Oct 18 '22 17:10

Jean-Baptiste Yunès


Firstly, run the action with a key so you can identify it later:

something.runAction(rotateAction, withKey: "rotate action")

Then you can stop it later by calling

something.removeActionForKey("rotate action")

Alternatively, you can remove all actions also

something.removeAllActions()
like image 36
ZeMoon Avatar answered Oct 18 '22 18:10

ZeMoon