05. Working with Multiple Levels

Introduction

Many tile-based games include multiple levels. This walk-through shows how to set up a multi-level game.

Review of Screens

So far our game has two screens:

  1. GameScreen – the base screen for our levels. It contains all common files, objects, and code
  2. Level1 – our first level. It contains files, objects, and code specific to level 1.

Although it took a little bit of time to set up two screens, we can now add additional levels very quickly.

Adding Level2 Screen

To add a new level we will create a new screen:

 

  1. Right-click on GameScreen in Glue
  2. Select Create Derived (Level) Screen
  3. Enter the name Level2 and click OK

 

Now our game has two levels: Level1 and Level2. We can choose which level we want to start on by right-clicking on a level and selecting Set as StartUp Screen.

Level2 will need its own TMX file, just like Level1. We have a few options on how to create Level2’s tmx:

  • Create a new TMX file in Tiled and save it in Level2’s content folder
  • Create a new TMX in Glue through the right-click option on Level2’s Files in Glue
  • Create a copy of Level1’s TMX file for Level2

We’ll use the third option as it will immediately give us a usable level:

  1. Expand Level1
  2. Right-click on Level1’s Files folder
  3. Select View in explorer

  4. Expand Level2
  5. Right-click on Level2’s Files folder
  6. Select View in explorer
  7. Copy Level1Tmx.tmx
  8. Paste it in Level2’s folder
  9. Rename the pasted file to Level2Tmx.tmx
  10. Drag+drop the newly copied (and renamed) Level2Tmx.tmx into Level2 in Glue

Setting Level2 Objects

Next we’ll assign the TileShapeCollection in Level2  just like we did for Level1:

  1. Expand Level2’s Objects folder
  2. Select the (yellow) SolidCollision object
  3. Select the Properties tab
  4. Scroll down to the bottom
  5. Change SourceType to File
  6. Change SourceFile to Screens/Level2/Level2Tmx.tmx
  7. Change SourceName to Tile Layer 1 (FlatRedBall.TileCollisions.TileShapeCollection)

Now we can run our game and Level2 will load and function just like Level1. Feel free to modify Level2Tmx.tmx in Tiled to differentiate between the two levels.

Switching Levels

We can switch between levels by switching between the screens for each level, since there is no difference between a level screen and a regular screen. In an actual game switching between levels may happen if a user collides with an object (such as a door) or if all enemies have been killed. For the sake of simplicity we’ll switch between levels whenever the space bar is pressed.

Note that the logic for switching between levels can be customized per-level, but we’ll be using nearly identical code for both.

We can switch from Level2 to Level1 by modifying Level2’s CustomActivity :

Similarly, we can modify Level1’s CustomActivity to go back to Level2 whenever the space bar is pressed again:

Now we can run the game and press the space bar to switch between levels.