如果我触摸和在SpriteKit Swift中停止触摸动作停止时如何重复动作

时间:2015-08-03 07:56:15

标签: ios swift sprite-kit skaction

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

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)

准确描述我想要的内容:

  

当我触摸屏幕时,会创建一个物体并旋转并保持旋转,直到我从屏幕上松开手指并停止触摸我希望它上升

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在您的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()停止特定于精灵的操作。

相关问题