richari1987
richari1987

Reputation: 360

UISwipeGestureRecognizer with SpriteKit using Swift

I am creating a game in swift that involves making words out of letters. The letters, which are individual SKSpriteNodes, sit on a "shelf" which is a SKSpriteNode. To remove the letters from the "shelf", I am trying to introduce swiping up. Unfortunately I am having issues with the swipe being picked up by the letters. The shelf seems to be absorbing it, or sometimes even the SKScene. I have disabled the user interaction on the shelf node.

This is my setup:

swipeRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("move:"))
swipeRecognizer.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeRecognizer)

I add the swipeRecognizer to the view, then in the move method, I have the following:

func move(swipe:UISwipeGestureRecognizer){        
    if(swipe.state == UIGestureRecognizerState.Ended && swipe.numberOfTouches() == 1){

        var touchLocation = swipe.locationInView(swipe.view)
        touchLocation = self.convertPointFromView(touchLocation)            
        var sprite = self.nodeAtPoint(touchLocation)

        if sprite is Letter{
            let letter = sprite as Letter
            if(gameManager.letterOnShelf(letter)){
                gameManager.letterFlicked(letter)
            }
        }
    }
}

Only sometimes to it recognize the sprite as a letter, 80% of the time the sprite is the shelf and I can't figure out what the correct way to work it is.....

Any help is greatly appreciated.

Cheers all!!

Upvotes: 1

Views: 1985

Answers (1)

Acey
Acey

Reputation: 8116

I would recommend:

  1. Using a UILongPressGestureRecognizer with minimumPressDuration set to 0.
  2. When the gesture is at the UIGestureRecognizerStateBegan state, see if the sprite your touch is over is a Letter and save a reference to that tile.
  3. In the UIGestureRecognizerStateEnded state, you should be able to see what location your gesture ended (or perhaps the trajectory, etc) and then move the tile you stored a reference to from earlier.

Upvotes: 1

Related Questions