2016-06-10 77 views
0

我在屏幕右側有一個向右移動的屏幕,當我按下屏幕左側時,屏幕上會出現一個圓圈。現在我需要做到這一點,當我開始觸摸時,我的圓圈向右移動,但是如果我釋放觸摸,那麼圓圈將停止,直到我再次按下屏幕。只有在按下時纔會移動Sprite?

這是在我的touchesBegan函數。

for touch: AnyObject in touches { 


    let location = touch.locationInNode(self) 
    if location.x < self.size.width/2 { 

     let moveLeft = SKAction.moveToX(self.frame.width/3, duration: 1.0) 
     Ball.runAction(moveLeft) 

    } 
    else { 

     let moveRight = SKAction.moveToX(self.frame.width/1.445, duration: 1.0) 
     Ball.runAction(moveRight) 


     } 





    } 

這是我到目前爲止。

回答

0

基本上是這樣的:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     if location.x < self.size.width/2 { 

      let moveLeft = SKAction.moveToX(self.frame.width/3, duration: 1.0) 
      Ball.runAction(moveLeft, withKey:"moveLeft") 
     } 
     else { 
      let moveRight = SKAction.moveToX(self.frame.width/1.445, duration: 1.0) 
      Ball.runAction(moveRight withKey:"moveRight") 
     } 
    } 
} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    Ball.removeActionForKey("moveLeft") 
    Ball.removeActionForKey("moveRight") 
} 

我建議你會處理的按鍵比硬編碼它們作爲字符串每次你需要的時候更聰明瞭一點,但是這是要點。

相關問題