2017-02-11 75 views
0

我正在嘗試使touchSBegin中的精靈向左移動,然後在用戶下一次觸摸時向右移動。點擊時雪碧方向改變?

我已經看到了一些代碼,我認爲會很好地工作,但是我也不太清楚如何界定「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 


} 

回答

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