How can I distinguish between gesture and touch in spriteKit and quickly?

I have two functions: one is activated when I touch the scene, and the other when I make a gesture, but when I make a gesture, the scene detects a touch and performs both functions

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    // physics & collisions
    physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    physicsWorld.contactDelegate = self

    // Swipe down
    let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
    swipeDown.direction = .Down
    view.addGestureRecognizer(swipeDown)

}

//MARK: Swipes
func swipedDown(sender:UISwipeGestureRecognizer){
    //first function
    swipeDown()
}

//MARK: Touches
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)
        // second function
        attack()
    }
}

}
+4
source share
2 answers

Instead, touchesBegantry using the click gesture ( UITapGestureRecognizer) for the attack action.

This is not necessary, but if you still have conflicts between the napkin and the tap, make the hard drive gesture dependent on the napkin's failure with the help requireGestureRecognizerToFailto make sure that the tap is not detected during the napkin.

+1
source

You can try something like this

var swiped = Bool()
func swipedDown(sender:UISwipeGestureRecognizer){
     //first function
     swipeDown()
     swiped = true 

}

Override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

     for touch in touches {
         let location = touch.locationInNode(self)
         // second function
         if swiped == false {
               attack()
         }
     }
}


 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)  {
      for touch in touches {
            swiped = false
       }

}

, .

+1

Source: https://habr.com/ru/post/1619780/


All Articles