停止正在运行的SKAction - Sprite Kit

时间:2022-10-28 20:11:57

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

2 个解决方案

#1


12  

You just have to use either:

你只需要使用:

  1. something.paused = false // or true to pause actions on the node
  2. something.paused = false //或true表示暂停节点上的操作
  3. something.removeAllActions() to definitely remove actions associated to the node
  4. something.removeAllActions()肯定删除与节点关联的操作
  5. name your action when launching something.runAction(action,withKey:"action1") and then something.removeActionForKey("action1") to remove a given action
  6. 在启动something.runAction(action,withKey:“action1”)然后something.removeActionForKey(“action1”)删除给定的动作时命名你的动作

You may also change the speed if needed.

如果需要,您也可以更改速度。

#2


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

#1


12  

You just have to use either:

你只需要使用:

  1. something.paused = false // or true to pause actions on the node
  2. something.paused = false //或true表示暂停节点上的操作
  3. something.removeAllActions() to definitely remove actions associated to the node
  4. something.removeAllActions()肯定删除与节点关联的操作
  5. name your action when launching something.runAction(action,withKey:"action1") and then something.removeActionForKey("action1") to remove a given action
  6. 在启动something.runAction(action,withKey:“action1”)然后something.removeActionForKey(“action1”)删除给定的动作时命名你的动作

You may also change the speed if needed.

如果需要,您也可以更改速度。

#2


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