2016-10-24 8 views
1

私のゲームでは、ノードをドラッグ、スワイプ、フリックすることができます。しかし、私はユーザーにノードをスワイプ/フリックできるようにしたいので、ユーザーがノードをドラッグできるようにしたくありません。これどうやってするの?ここでスプライトをドラッグせずにフリック/スワイプするにはどうすればよいですか?

は私の現在のコードです:

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

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

     if ball.frame.contains(location) { 
      touchPoint = location 
      touching = true 
     } 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first as UITouch! 
    let location = touch!.location(in: self) 
    touchPoint = location 

    for touch in touches { 
     let location = touch.location(in: self) 
     touchPoint = location 
     if touching { 
      if touchPoint != ball.position { 
       let dt:CGFloat = 1.0/60.0 
       let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y) 
       let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt) 
       ball.physicsBody!.velocity=velocity 
      } 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    touching = false 

    for touch in touches { 
     let location = touch.location(in: self) 
     touchPoint = location 
     let node = self.atPoint(location) 
    } 
} 
+0

達成したいことを明確にしてください。 – matthiaswitt

+0

あなたの目標は、指がスワイプジェスチャーから解放されるまでノードが動かないようにすることです。その時点で、指の動きの速度と距離が画面全体でノードの移動速度と方向を決定します。 – Confused

答えて

1

UIGestureRecognizersが存在する理由です。あなたの視野にUIPanGestureRecognizerを追加し、velocity(in:UIView)メソッドを使用して、フリックの方向とスピードを教えてください。

ジェスチャだけを使用する場合は、ジェスチャをノードにのみ適用することはできません。ビューのみからビュー座標をシーン座標に変換する必要があります。

ジェスチャーのタッチがシーンに流れ込むようにしたり、通常のタッチ呼び出しを許可したり、パンジェスチャーのベロシティを使用することもできます。

https://developer.apple.com/reference/uikit/uipangesturerecognizer

関連する問題