3

I'm working with SpriteKit and wondering if anyone knows how to make a sprite follow a path. I've seen other questions online that talk about moving a sprite along a CGPath, but how would you do it if the path is another sprite?

Video example

Otto Gutierrez
  • 447
  • 2
  • 5
  • 15
  • What about `SKAction.followPath()` ? – Martin R May 30 '17 at 22:01
  • hi @MartinR, yes I have tried that, but I have to provide a CGPath, and that is my question, how could I get a CGPath from the sprite. – Otto Gutierrez May 31 '17 at 11:28
  • Then I misunderstood your question, sorry! – Martin R May 31 '17 at 11:30
  • You would need to define / convert the roadmap sprite into a CGPath somehow. Doing this automatically is is akin to image recognition and could be quite complex. – Alain T. May 31 '17 at 11:37
  • hi @AlainT. , Just imagine a street (sprite) and a car (another sprite), so I want the car to move following the street. I posted a youtube video at the bottom of my question so it's better to understand. Thank you. – Otto Gutierrez May 31 '17 at 11:38
  • Is there some reliable pattern (e.g. colours) that distinguishes parts of the roadmap sprite's texture that are on the road from parts that are off the road ? It there is something like that, I could think of a few strategies to make the "car" sprite follow the road. – Alain T. May 31 '17 at 12:00

1 Answers1

1

Have a look at Is there a way to create a CGPath matching outline of a SKSpriteNode? - it seems from that answer that the problem is much more complex than it first appears.

If your sprite is an SVG, a simpler solution may be to manually convert the SVG into a CGPath, and then using that CGPath in your game. A simple tool for doing that is available here: https://swiftvg.mike-engel.com/. Of course doing this manually won't scale well if you have many different sprites.

This may not be exactly the answer you are looking for, but it seems right now there is no simple way to accomplish what you need :)

JohnV
  • 981
  • 2
  • 8
  • 18