Stopping an running SKAction - Sprite Kit

百般思念 提交于 2020-01-01 08:36:09

问题


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()
}

回答1:


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.




回答2:


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()


来源:https://stackoverflow.com/questions/35502326/stopping-an-running-skaction-sprite-kit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!