tvos swift spritekit - UITapGestureRecognizer not responding

339 views Asked by At

I have not been able to get this gesture to respond, at all.

    self.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.movePlayer(_:)))
    tap.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
    view?.addGestureRecognizer(tap)

and the function:

func movePlayer(_ sender: UITapGestureRecognizer){
    player.run(SKAction.moveBy(x: 200, y: 0, duration: 10))
    print("Right!")
}

nothing at all happens when pressing the play button on the simulator remote. What am I missing?

1

There are 1 answers

4
crashoverride777 On BEST ANSWER

The tap gesture recogniser code looks fine. Where are you adding it, is it in didMoveToView?

I was recently playing around with the new iOS 10

override func sceneDidLoad() { }

method and I noticed that gesture recognisers added there do not work.

This works in the simulator

 class GameScene: SKScene, SKPhysicsContactDelegate {

      override func didMove(to view: SKView) {

          self.isUserInteractionEnabled = true
          let tap = UITapGestureRecognizer(target: self, action: #selector(self.movePlayer(_:)))
          tap.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
          view.addGestureRecognizer(tap)
     }

     func movePlayer(_ sender: UITapGestureRecognizer){
         print("Right!")

     }
 }

Hope this helps