【发布时间】: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