How to add a swipe gesture to a node in spritekit

2019-08-01 02:31发布

问题:

I'm trying to add a swipe gesture to a node so that when a user swipes it, it goes off screen but I keep getting a SIGABRT error:

`Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[fidget2.PlankScene swipedRight:]: unrecognized selector sent to instance 0x7ff4c3603e00'`

I'm not sure why this error is popping up. I made sure the node is labeled correctly in the .sks file. Here is my code:

import SpriteKit


let plankName = "woodPlank"

class PlankScene: SKScene {

  var plankWood : SKSpriteNode?


  override func didMove(to view: SKView) {

    plankWood = childNode(withName: "woodPlank") as? SKSpriteNode


    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))

    swipeRight.direction = .right

    view.addGestureRecognizer(swipeRight)

  }


  func swipedRight(sender: UISwipeGestureRecognizer) {

    print("Object has been swiped")

  }


 func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
  {
    let touch = touches.first as! UITouch
    let location = touch.location(in: self)

    if (plankWood?.frame.contains(location))!
    {
     print("Swipe has started")
    }
  }
}

回答1:

Had the same problem, just so we have an accepted answer on this I would like to point out that the comment left by 0x141E is the correct solution to this:

Replace Selector("swipedRight:") with #selector(PlankScene.swipedRight)