我试图将SKNode作为函数的参数传递,因此我可以访问SKNode的属性,例如函数中的position / p。
class GameScene: SKScene {
let node = SKShapeNode()
let swipeRightRec = UISwipeGestureRecognizer()
var pathToDraw = CGMutablePath()
override func didMove(to view: SKView) {
pathToDraw.move(to: CGPoint(x: 100.0, y: 100.0))
pathToDraw.addLine(to: CGPoint(x: 125.0, y: 50.0))
node.path = pathToDraw
//trying to pass created node in the swipedRight function
swipeRightRec.addTarget(self, action: #selector(GameScene.swipedRight(node)))
swipeRightRec.direction = .right
self.view!.addGestureRecognizer(swipeRightRec)
}
// the swipe right function that accepts a SKShapeNode
@objc func swipedRight(node: SKShapeNode) {
let path = node.path
}
}
我收到的错误是:
实例成员“ swipedRight”不能用于“ GameScene”类型;您的意思是改用这种类型的值吗?
最佳答案
在目标/动作模式中,任何动作的(第一个)参数都必须是执行该动作的对象
@objc func swipedRight(_ sender: UISwipeGestureRecognizer) { ... }
并添加选择器和方法名称
swipeRightRec.addTarget(self, action: #selector(swipedRight))
但是由于节点仍然是属性,因此不必将其作为参数传递。