0

I have been struck with the famous "unrecognized selector sent to instance". The full error is

-[MyGame.GameScene handlePanFrom:]: unrecognized selector sent to instance 0x103c05fe0

The function that is referring to is her in the game scene:

    func handlePanFrom(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .began {
        var touchLocation = recognizer.location(in: recognizer.view)
        touchLocation = self.convertPoint(fromView: touchLocation)

        self.selectNodeForTouch(touchLocation: touchLocation)
    } else if recognizer.state == .changed {
        var translation = recognizer.translation(in: recognizer.view!)
    translation = CGPoint(x: translation.x, y: -translation.y)

        self.panForTranslation(translation: translation)

        recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
    } else if recognizer.state == .ended {
    if selectedPlayer.name != kAnimalNodeName {
      let scrollDuration = 0.2
        let velocity = recognizer.velocity(in: recognizer.view)
      let pos = selectedPlayer.position

      // This just multiplies your velocity with the scroll duration.
      let p = CGPoint(x: velocity.x * CGFloat(scrollDuration), y: velocity.y * CGFloat(scrollDuration))

      var newPos = CGPoint(x: pos.x + p.x, y: pos.y + p.y)
        newPos = self.boundLayerPos(aNewPosition: newPos)
      selectedPlayer.removeAllActions()

        let moveTo = SKAction.move(to: newPos, duration: scrollDuration)
        moveTo.timingMode = .easeOut
        selectedPlayer.run(moveTo)
    }
  }
}

And then this code in the game scene did move function

    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handlePanFrom:")))
    self.view!.addGestureRecognizer(gestureRecognizer)

If you need to see any of the other code I will provide.

It should also be noted that I changed some code in the GameViewController but I don't know if that has anything to do with it.

Evonic
  • 25
  • 1
  • 4
  • I'm not familiar with SpriteKit but you probably need to declare handlePanFrom: as an @objc func to use selectors with it. – Kaom Te Aug 20 '20 at 23:54
  • @KaomTe Thanks for trying, but that doesn't work. Overall this function mixed with some other code is supposed to move my sprite any where when I swipe. The game freezes when I touch it and I get the same error. – Evonic Aug 20 '20 at 23:58
  • 1
    Just want to mention, I've never created selectors using the Selector constructor that way in swift. I believe #selector is the better safer way to reference selectors. See here: https://stackoverflow.com/questions/24007650/selector-in-swift – Kaom Te Aug 21 '20 at 00:10

1 Answers1

0

Try the #selector syntax, like so :

let gestureRecognizer = UIPanGestureRecognizer( target: self, action: #selector(handlePanFrom(recognizer:)) )

and mark the method as @objc

@objc func handlePanFrom(recognizer: UIPanGestureRecognizer) {
    print("Pan !")
}
TheAppMentor
  • 1,091
  • 7
  • 14