1

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?

Zek
  • 155
  • 7

1 Answers1

0

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

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • ....Yes, it's in the sceneDidLoad() function. I tried moving it to didMove instead, still doesn't work. – Zek Nov 12 '16 at 00:33
  • You need didMove for gestures, I remember testing with sceneDidLoad and it doesnt work. I just tested this with a new project and it works fine. Can you update your question with the whole code of your scene. – crashoverride777 Nov 12 '16 at 00:50
  • Well, I tried testing it in a new project and now it's working. Moving my code to didMove does work. I also moved the sprite placement to didMove which fixed another error I was having where it created a copy of the sprite that moved instead. So, sceneDidLoad is just terrible or something? – Zek Nov 15 '16 at 19:36
  • Hey, yeah sceneDidLoad is weird, I guess it gets called before didMoveToView which is why recognisers don't work. Thanks for marking. Happy coding – crashoverride777 Nov 16 '16 at 10:56