我正在构建一种绘图功能,我将本教程用作参考:https://www.raywenderlich.com/80586/make-line-drawing-game-sprite-kit-swift。令我困惑的是,我在玩了一段时间后得到错误 - 而不是一开始。这些是我用于绘图的两个函数:
func createPathToMove() -> CGPathRef? {
if wayPoints.count <= 1 {
return nil
}
let ref = CGPathCreateMutable()
for i in 0 ..< wayPoints.count {
let p = wayPoints[i]
if i == 0 {
CGPathMoveToPoint(ref, nil, p.x, p.y)
} else {
CGPathAddLineToPoint(ref, nil, p.x, p.y)
}
}
return ref
}
func drawLines() {
enumerateChildNodesWithName("line", usingBlock: {node, stop in
node.removeFromParent()
})
if let path = createPathToMove() {
let shapeNode = SKShapeNode()
shapeNode.path = path
shapeNode.name = "line"
shapeNode.strokeColor = UIColor.grayColor()
shapeNode.lineWidth = 2
shapeNode.zPosition = 1
self.addChild(shapeNode)
}
}
在这里我调用drawLines()函数:
func moveSprite(sprite: SKSpriteNode, velocity: CGPoint) {
let amountToMove = CGPoint(x: velocity.x * CGFloat(dt),
y: velocity.y * CGFloat(dt))
sprite.position = CGPoint(
x: sprite.position.x + amountToMove.x,
y: sprite.position.y + amountToMove.y)
wayPoints.append(sprite.position)
drawLines()
}
任何帮助都将受到赞赏,因为我找不到任何有效的解决方案。