Search code examples
swiftsprite-kitcgpathskshapenode

Sprite Kit: How to create infinite game background like 'Binary Rush' or 'Stay in the line'?


I want to create an infinite game background like 'Binary Rush' or 'Stay in the line'. The background could be a simple track. It's sort of like a random racing road track.

Currently I have used a single SKShapeNode to draw the path, and just set the lineWidth to a big number. I use var path = CGPathCreateMutable(); to create the random track. Apparently this is not the ideal solution since the track is restricted to a single line. I would like the track could be expanded with different width or even add some obstacles inside the track.

So my questions are: 1. For those two game, are their backgrounds just images, and when scrolling, it just load a random one? 2. How could I random generate those tracks, with the flexibility to change its width or add obstacles inside them? 3. How to combine the different segments of those tracks, I assume not to create an infinite long path. Should split it into different segments and removeFromParent once the segment is scrolling off screen.

Thanks a looooooooooooooot!!! :)


Solution

  • Use the method you are using with ShapeNode. Instead of making the line width big, make another line that is essentially a copy of first line that is shifted over a particular amount of pixels. That will give you boundaries (like a road). You can set collision and contact events to prevent the car from leaving the road or for damaging a car that hits a wall, etc.

    How many pixels you shift the second wall over will let you control the width. You can easily make the track get wider or slimmer depending on the shift.

    Create an algorithm that randomly places objects in the track. You could use the two lines that are boundaries to calculate where you should spawn these objects.