点击时精灵方向改变?

Sprite direction change when tapped?

我试图让精灵在 touchesBegan 时向左移动,然后在用户下次触摸时向右移动。

我看过一些我认为可以完美运行的代码,但是我不太确定如何定义 "isMovingleft"

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    ship.removeAllActions()

    if isMovingleft == true  {
        let left = SKAction.moveBy(x: 500, y: 0, duration: 5)
        ship.run(left)
    }
    else {
        let right = SKAction.moveBy(x: -500, y: -900, duration: 5)
        ship.run(right)
    }
    isMovingleft = !isMovingleft


}
enum Direction: Int {
    case left = 0
    case right
}

var direction: Direction?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    ship.removeAllActions()

    switch direction ?? .left {
    case .left:
        ship.run(SKAction.moveBy(x: 500, y: 0, duration: 5))
        direction = .right
    case .right:
        ship.run(SKAction.moveBy(x: -500, y: -900, duration: 5))
        direction = .left
    }
}