2017-01-04 5 views
0

私は既に自分のプレイヤーの動きを検出するためにジェスチャーリコナイザーをゲームに実装していますが、私には自分が望む結果を与えていないことがわかっているので、スワイプジェスチャーtouchesメソッドとタップメソッドでも同様です。私はタッチメソッドでタップ機能を動作させることに成功しましたが、タッチメソッドでスワイプ機能を実装することができず、これを行う方法に関するチュートリアルを見つけることができません。どのように私はタッチ方式は、特定の方向にスワイプジェスチャを認識させることができスワイプでのスワイプジェスチャー

class GameScene: SKScene { 

var touchOrigin = CGPoint() 
var player = SKSpriteNode() 


override func didMove(to view: SKView) { 

    backgroundColor = .black 

    player = SKSpriteNode(texture: nil, color: .orange, size: CGSize(width: 50, height: 50)) 
    player.position = CGPoint(x: 0, y: 0) 
    addChild(player) 

} 

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

} 

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

} 

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

    for touch in touches { 

     var currentTouchPosition = touch.location(in: self) 
     touchOrigin = touch.location(in: self) 

     if (Int(touchOrigin.x) > Int(currentTouchPosition.x)) { 

      player.position.x -= 50 

     } else if (Int(touchOrigin.x) < Int(currentTouchPosition.x)) { 

      player.position.x += 50 

     } 

     if touch.tapCount == 1 { 

      player.position.y += 50 //replace this with function :) 

     } else if touch.tapCount >= 2 { 

      player.position.y += 150 // change to run shield effect :) 

     } 
    } 

} 

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

} 

:以下の私のコードは、私はこれを試してみて、達成するために使用していますタッチ方法を示して?それらがある方向にスワイプして画面から指を離して1つのモーションで原点にスワイプすると、どのようにしてそれをタップとして認識できるのですか?

答えて

2

スワイプジェスチャーを検出する方法の例を次に示します。

まず、開始位置を格納するためにインスタンス変数を定義し、時間

var start:(location:CGPoint, time:TimeInterval)? 

及びスワイプジェスチャーのパラメータを定義します。 touchesBegan

let minDistance:CGFloat = 25 
let minSpeed:CGFloat = 1000 
let maxSpeed:CGFloat = 6000 

を、touchesEnded

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     start = (touch.location(in:self), touch.timestamp) 
    } 
} 

各新しいタッチイベントの位置/時間を節約し、ユーザのジェスチャは、距離と速度を比較することにより、スワイプであったかどうかを決定する。従ってこれらを調整しますジェスチャー。対角線スワイプのテストはオプションです。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    var swiped = false 
    if let touch = touches.first, let startTime = self.start?.time, 
      let startLocation = self.start?.location { 
     let location = touch.location(in:self) 
     let dx = location.x - startLocation.x 
     let dy = location.y - startLocation.y 
     let distance = sqrt(dx*dx+dy*dy) 

     // Check if the user's finger moved a minimum distance 
     if distance > minDistance { 
      let deltaTime = CGFloat(touch.timestamp - startTime) 
      let speed = distance/deltaTime 

      // Check if the speed was consistent with a swipe 
      if speed >= minSpeed && speed <= maxSpeed { 

       // Determine the direction of the swipe 
       let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0 
       let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0 

       swiped = true 
       switch (x,y) { 
       case (0,1): 
        print("swiped up") 
       case (0,-1): 
        print("swiped down") 
       case (-1,0): 
        print("swiped left") 
       case (1,0): 
        print("swiped right") 
       case (1,1): 
        print("swiped diag up-right") 
       case (-1,-1): 
        print("swiped diag down-left") 
       case (-1,1): 
        print("swiped diag up-left") 
       case (1,-1): 
        print("swiped diag down-right") 
       default: 
        swiped = false 
        break 
       } 
      } 
     } 
    } 
    start = nil 
    if !swiped { 
     // Process non-swipes (taps, etc.) 
     print("not a swipe") 
    } 
} 
関連する問題