danielpp95
danielpp95

Reputation: 126

how i can differentiate between a gesture and a touch in spriteKit and swift?

i have two functions, one is activated when i touch the scene and another one when i make a gesture down, but when i make a gesture the scene detect a touch and execute 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()
    }
}

}

Upvotes: 4

Views: 1283

Answers (2)

Pranav Wadhwa
Pranav Wadhwa

Reputation: 7736

You could 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
       }

}

Hope this helps.

Upvotes: 1

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

Instead of touchesBegan, try using a tap gesture (UITapGestureRecognizer) for attack action.

This should not be necessary but if you still get conflict between swipe and tap, make the tap gesture depend on swipe's failure using requireGestureRecognizerToFail to ensure that a tap is not detected during a swipe.

Upvotes: 2

Related Questions