2

I'm trying to make a train track switching game and I've successfully drawn bezier paths and converted them to CGPath so that I can use SpriteKit's SKAction:

follow(_:asOffset:orientToPath:speed:)

This works fine for following any track (path) with orientation to the path for free. If my game had a single track that would be fine but I want to have railway switches to change tracks.

I drew the tracks so that they overlap on the straight and then branch off down the line. I figured I could switch tracks by checking if my sprite is on the overlapping path and make it follow the new path. Of course, this was naive and I only know how to use the "follow" action to follow a path from the start of the path, not from wherever my sprite happens to be. Redrawing the path as a subpath with the travelled path subtracted seems as difficult as the problem I'm currently tackling.

I'm beginning to think updating the sprite's position from

func update(_ currentTime: TimeInterval)

Does CGPath allow me to get an x coordinate for a given y on the path that I can update manually? Incidentally, I know I would lose path orientation with this method but I can ask another question about that.

OsakaStarbux
  • 147
  • 12
  • You can use [this](https://stackoverflow.com/questions/24274913/equivalent-of-or-alternative-to-cgpathapply-in-swift) to loop through the elements of a CGPath. The elements are high level commands (moveTo, addLineTo, addQuadCurveTo, etc.), so you'll need to implement code to determine the x and y values for points along a line, etc. – 0x141E Aug 08 '17 at 07:16

0 Answers0