Search code examples
swiftsprite-kitskshapenode

How to modify the path of SKShapeNode on the fly?


somewhere in didMove I am creating SKShapeNode :

path.move(to: points.first!)

let linearShapeNode = SKShapeNode()
linearShapeNode.path = path
linearShapeNode.strokeColor = .black
linearShapeNode.lineWidth = 3.0
linearShapeNode.lineCap = .round
someSprite.addChild(linearShapeNode)

path is class member:

var path: CGMutablePath

Then in update method of scene I am adding lines to the path:

path.addLine(to: points[index])

I expect to see new lines drawn on the sprite. However, no new lines appear. If after addLine I re-assign path to SKShapeNode's path, then I see it. But it doesn't look optimal to draw all over again.

What is the proper way of updating path on SKShapeNode? What I am trying to achieve is to see new line segment each half second.


Solution

  • You can't update the path, you can only replace it with a new path (even if it means taking a copy of the path, making it mutable, and then reassigning it back to your node)

    SKShapeNode uses a copy of the path you assign to it, so even if you use a mutable path, you will not see the changes.

    The easiest thing for you to do is after you make your path change, assign it once more to your shape.