2017-02-16 80 views
0

我試圖用一個ThumbStick和兩個按鈕來實現用戶控制節點。我創建了帶有控制元素的獨立SKSpriteNode,並覆蓋父節點的觸摸事件以處理用戶觸摸。touchesMoved在不移動手指的情況下調用

問題是,當我開始觸摸屏時,即使我不移動手指,touchesMoved也會被多次調用。

這裏是我的觸摸事件代碼:

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

    for touch in touches { 
     let touchPoint = touch.location(in: self) 

     touchStatusLabel.text = String(format: "TOUCH BEGAN %@", arguments:[NSStringFromCGPoint(touchPoint)]) 

     if aButton.frame.contains(touchPoint) { 
      NSLog("A BUTTON PRESSED") 
      delegate?.controlInputNode(self, beganTouchButtonWithName: aButton.name!) 
     } 
     else if bButton.frame.contains(touchPoint) { 
      NSLog("B BUTTON PRESSED") 
      delegate?.controlInputNode(self, beganTouchButtonWithName: bButton.name!) 
     } 

     else if touchPoint.x < 0 && touchPoint.y < 0 { 
      NSLog("THUMBSTICK PRESSED") 
      leftThumbStickNode.touchesBegan([touch], with: event) 
      leftThumbStickNode.position = pointByCheckingControlOffset(touchPoint) 
     } 
    } 
} 

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

    for touch in touches { 
     let touchPoint = touch.location(in: self) 
     touchStatusLabel.text = String(format: "TOUCH MOVED %@", arguments:[NSStringFromCGPoint(touchPoint)]) 

     if !aButton.frame.contains(touchPoint) && !bButton.frame.contains(touchPoint) { 
      if touchPoint.x < 0 && touchPoint.y < 0 { 
       leftThumbStickNode.touchesMoved([touch], with: event) 
      } 
     } 
    } 
} 

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

    for touch in touches { 
     let touchPoint = touch.location(in: self) 
     touchStatusLabel.text = String(format: "TOUCH ENDED %@", arguments:[NSStringFromCGPoint(touchPoint)]) 

     let node = atPoint(touchPoint) 

     if node == aButton || node == bButton { 
      delegate?.controlInputNode(self, endTouchButtonWithName: node.name!) 
     } 

     else { 
      leftThumbStickNode.touchesEnded([touch], with: event) 
     } 
    } 
} 

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

    NSLog("TOUCH CANCELED") 

    leftThumbStickNode.touchesCancelled(touches, with: event) 
} 

回答

0

它只出現在調試所以在出現這個問題,我認爲這是一些調試問題

0

您可以嘗試比較翻譯,如果一切都沒有改變,那麼請不要做任何事情,所以你touchesMoved功能看起來像這樣;

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

    for touch in touches { 
     let location = touch.location(in: self) 
     let previousLocation = touch.previousLocation(in: self) 

     let translation = CGPoint(x: location.x - previousLocation.x, y: location.y - previousLocation.y) 
     if translation == CGPoint.zero { 
      continue 
     } 

     touchStatusLabel.text = String(format: "TOUCH MOVED %@", arguments:[NSStringFromCGPoint(location)]) 

     if !aButton.frame.contains(location) && !bButton.frame.contains(location) { 
      if location.x < 0 && location.y < 0 { 
       leftThumbStickNode.touchesMoved([touch], with: event) 
      } 
     } 
    } 
} 
+0

對不起,反應遲緩。它沒有幫助,因爲觸摸位置已更改。我嘗試做出一些門檻,但它也沒有幫助,有時觸摸位置有很大的差異。我還發現touchBegan和touchEnded也被稱爲但很少 – morffy

相關問題