如何在我触摸时重复动作以及当我停止触摸时 SpriteKit 中的动作停止 Swift

How to repeat action as long I touch and when I stop touching the action stop in SpriteKit Swift

正如标题中所说,我想做一个动作 运行 并重复,只要我按下一个按钮并停止,当我停止触摸时 运行 另一个动作。我不知道这样做的方法所以如果有人可以帮助我编写代码例如我想要一个精灵 只要我按下就旋转,在我停止触摸后向上移动。

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1.0)
    sprite.runAction(SKAction.repeatActionForever(action))  
          let action2 = SKAction.moveToY(900, duration: 1.0)
            sprite.runAction(action2)

准确描述我想要的:

when I touch the screen an object will be created and rotate and keep rotating till I release my finger from the screen and stop touching I want it to go up

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    sprite.removeAllActions()
    let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1.0)
    sprite.runAction(action)
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    sprite.removeAllActions()
    let action2 = SKAction.moveToY(900, duration: 1.0)
    sprite.runAction(action2)
}

在您的 Sprite Kit 场景中,这些方法允许您检测触摸何时开始和结束:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    // Start an action that repeats indefinitely.
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    // Remove the action after touches end.
}

当触摸结束时,调用 self.removeAllActions() 停止场景中的所有动作,或 sprite.removeAllActions() 停止特定于精灵的动作。