我正在尝试使用SKShapeNode创建一个常量。我在最后一小时踢了这个1个错误。请帮帮我,我会很感激。
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()
}
}
答案 0 :(得分:0)
变量路径在addObstacle函数中不可见。 如果要将其用于其他函数,则将其声明为类var。
class GameScene: SKScene {
var path = UIBezierPath()
...
}