将SKNode作为函数的参数传递时出错

时间:2019-06-02 08:42:31

标签: swift sprite-kit

我试图将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'类型;您是不是要使用这种类型的值?

1 个答案:

答案 0 :(得分:0)

在目标/动作模式中,任何动作的(第一个)参数都必须是执行该动作的对象

@objc func swipedRight(_ sender: UISwipeGestureRecognizer) { ... }

并使用方法名称添加选择器

swipeRightRec.addTarget(self, action: #selector(swipedRight))

但是由于节点仍然是一个属性,因此不必将其作为参数传递。