Noah Covey
Noah Covey

Reputation: 161

SpriteKit & Swift: How to create level "segments" that are randomly "stitched" together to create an endless game?

The concept that I am talking about is similar to the style of game seen in many minimalistic, popular mobile games such as Color Switch, the Line Zen, Phases, or Bounce. These are endless games composed of a series of "levels" or "rooms" that are placed in a random order, and are one after another, creating the effect of an endless game. The key thing is that the challenges in each level are not random, they are drawn up before, and if that certain level is chosen randomly, it appears on the screen and the player moves through that level.

I think this concept might be called procedural generation, though I'm not positive.

How would I do this in SpriteKit using Swift? I'm not really sure where to start, maybe create a function for each level segment and, every few seconds, choose a random one to put on the screen?

Any help is appreciated!

Thanks so much!

Upvotes: 2

Views: 728

Answers (1)

Dion Larson
Dion Larson

Reputation: 878

Procedural generation is the name of the concept you are describing.

The approach will change a bit depending on the type of game you are trying to make but let's look at procedural generation in an infinite runner game. What you want to do is set up a buffer of level segments. The total size of the segments in your buffer should be at least twice the size of the screen. Every segment should be a child of the same segments node and they should be positioned so that each starts right after the previous one ends.

When a segment moves off screen (the player passed it):

  • remove that segment from the segments node
  • initialize a new segment (probably from a sks file)
  • add the new segment to the segments node
  • position it behind the last segment in the segments node.

The logic you use for choosing the next "random segment" is up to you. It can be truly random or you can fine tune it for the best user experience (avoid repeating segments, avoid segments that would ruin the flow, etc).

The key is to remove segments as they go offscreen and add a new one at the end of the buffer. This must be position based, not time based (time is less reliable even when the game scrolls at a constant speed).

Upvotes: 2

Related Questions