我创建了一个移动射击电子游戏,我想在每个行星之间建立一条线(总共8个行星)以形成一条路径,但是我不明白为什么我的线没有出现。 这是我的代码: 我想说的是,我从Swift和Spritekit开始。 here is a screenshot
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: size.width * 0.3, y: size.height * 0.9))
bezierPath.addLine(to: CGPoint(x:size.width/2 , y: self.size.height/1.22))
bezierPath.addCurve(to: CGPoint(x: 0, y: 12.0),
controlPoint1: CGPoint(x: 2, y: 14),
controlPoint2: CGPoint(x: 0, y: 14))
bezierPath.addLine(to: CGPoint(x:size.width * 0.3 , y: self.size.height/1.22))
bezierPath.addLine(to: CGPoint(x: 210.5, y: 192.5))
bezierPath.addLine(to: CGPoint(x: 67.5, y: 222.5))
bezierPath.addLine(to: CGPoint(x: 33.5, y: 291.5))
bezierPath.addLine(to: CGPoint(x: 78.5, y: 379.5))
bezierPath.addLine(to: CGPoint(x: 210.5, y: 421.5))
UIColor.red.setStroke()
bezierPath.lineWidth = 1
bezierPath.stroke()
let neptune = SKSpriteNode(imageNamed: "Neptune")
neptune.anchorPoint = CGPoint(x: 0.5, y: 0.5)
neptune.setScale(0.45)
neptune.position = CGPoint(x: size.width * 0.3 , y: size.height * 0.9)
neptune.zPosition = 2
neptune.name = "bouton1"
self.addChild(neptune)
let uranus = SKSpriteNode(imageNamed: "Uranus")
uranus.anchorPoint = CGPoint(x: 0.5, y: 0.5)
uranus.setScale(0.35)
uranus.position = CGPoint(x: self.size.width/2, y:
self.size.height/1.22)
uranus.zPosition = 2
uranus.name = "bouton1"
self.addChild(uranus)
答案 0 :(得分:0)
仅当您具有活动的渲染上下文时,尝试笔触路径的方式才有效。 在SpriteKit中,如果要在场景中渲染路径,则应从该路径创建一个SKShapeNode并将其添加到场景中:
var shape = SKShapeNode()
shape.path = bezierPath.CGPath
shape.strokeColor = SKColor.red
self.addChild(shape)