【问题标题】:How to add a swipe gesture to a node in spritekit如何在 spritekit 中向节点添加滑动手势
【发布时间】:2016-09-30 04:17:58
【问题描述】:

我正在尝试向节点添加滑动手势,以便当用户滑动它时,它会离开屏幕,但我不断收到 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")
    }
  }
}

【问题讨论】:

  • #selector(PlankScene.swipedRight)替换Selector("swipedRight:")
  • 验证 didMove 只被调用一次,并删除你的 deinit 中的手势。
  • 手势识别器不会添加到节点,而是添加到视图...
  • 顺便说一句(与您的问题无关),您设置了一个常量 plankName,但无论如何都要使用字符串文字调用 childNode()。

标签: ios swift sprite-kit skspritenode skscene


【解决方案1】:

遇到了同样的问题,所以我们对此有一个公认的答案我想指出0x141E留下的评论是解决这个问题的正确方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多