如何在spritekit中向节点添加滑动手势

时间:2016-09-30 04:17:59

标签: ios swift sprite-kit skspritenode skscene

我正在尝试向节点添加滑动手势,以便当用户滑动时,它会离开屏幕,但我不断收到SIGABRT错误:

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

我不确定为什么会出现这个错误。我确保在.sks文件中正确标记了节点。这是我的代码:

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 个答案:

答案 0 :(得分:4)

出现同样的问题,我们就此得到了一个可接受的答案,我想指出0x141E留下的评论是对此的正确解决方案:

Selector("swipedRight:")替换为#selector(PlankScene.swipedRight)