Search code examples
swiftskshapenode

Use of unresolved identifier 'path' SKShapeNode


I am trying to create a constant using SKShapeNode. I have this 1 error kicking my ass for the last hour. Please help me out, I will appreciate it.

import SpriteKit

class GameScene: SKScene {
    override func didMove(to view: SKView) {

        func setupPlayerAndObstacles() {
            addObstacle()
        }

        func addObstacle() {
            addCircleObstacle()

            let section = SKShapeNode(path: path.cgPath) //this line is showing error. Use of unresolved identifier 'path'//
            section.position = CGPoint(x: size.width/2, y: size.height/2)
            section.fillColor = .yellow
            section.strokeColor = .yellow
            addChild(section)
        }

        func addCircleObstacle() {

            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y: -200))
            path.addLine(to: CGPoint(x: 0, y: -160))
            path.addArc(withCenter: CGPoint.zero, radius: 160, startAngle: CGFloat(3.0 * .pi / 2), endAngle: CGFloat(0), clockwise: true)
            path.addLine(to: CGPoint(x: 200, y: 0))
            path.addArc(withCenter: CGPoint.zero, radius: 200, startAngle: CGFloat(0.0), endAngle: CGFloat(3.0 * .pi / 2), clockwise: false)

        }

        setupPlayerAndObstacles()

    }
}

Solution

  • The variable path is not visible in addObstacle function. Declare it as class var if you are going to use it en other functions.

    class GameScene: SKScene {
    var path = UIBezierPath()
     ...
    }