【问题标题】:Sprite direction change when tapped?点击时精灵方向改变?
【发布时间】:2017-06-29 18:37:32
【问题描述】:

我试图让精灵在 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


}

【问题讨论】:

    标签: ios swift sprite skaction


    【解决方案1】:
    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      相关资源
      最近更新 更多