2017-05-28 65 views
-1

在我的遊戲中,一旦你點擊屏幕,玩家開始向右移動。如果再次點擊屏幕,播放器會向下移動。這裏是一個代碼有點如何在點擊屏幕時更改物理體的向量?

var isMoving = true 

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



    //moves to the right 
    if isMoving == true { 
     player.physicsBody?.velocity = CGVector(dx: 600.0 + playerSpeed, dy: 0) 


    } 
    //moves down 
    else { 

     player.physicsBody?.velocity = CGVector(dx: 0, dy: -600.0 - playerSpeed) 

    } 

    isMoving = !isMoving 

} 

我在這裏的問題是,我想玩家的方向從下到左側改變,進而形成從左到右。所以序列將是正確的,向下的,向左的和向上的。換句話說,當屏幕被點擊時總是右轉。

回答

0

這樣做的一種方法是獲取不同方向(矢量)的列表。您可以保留一個索引,並在每次敲擊時增加一個索引,並使用nDirections模。然後你更新玩家的方向。