2017-02-16 4 views
0

私は1つのThumbStickと2つのボタンでユーザーコントロールノードを実装しようとしています。私はそれにコントロール要素を持つ別のSKSpriteNodeを作成し、親ノードのタッチイベントをオーバーライドしてユーザーのタッチを処理します。touchesMovedは指を動かさずに呼び出されました

私がタッチスクリーンを起動すると、指を動かさなくても何度も呼び出されるtouchesMoved getが問題になります。ここで

は私のタッチイベントコードです:

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

関連する問題