-
As we've done before, we'll create a function again. This time, it will hold all relevant code that will create a new level in our 2D sidescrolling platformer game in MakeCode Arcade. Make a new function and name it createLevel.Â
-
Take note that the function type used here accepts an integer number argument, num. This num will be used to choose from different level maps and backgrounds.
-
So far, we've used arrays for multiple enemy sprites as in our version of Space Invaders. This time, we'll use an array to hold the tile maps for all the four levels of our game.
Create an array and name it levelTileMaps, then duplicate the first level's tile map (from on start block) and place it in the array. That will be the first element of our array, an index 0.Â
-
Duplicate the tile map three times, then edit them in the sprite editor to change the positions of the tiles. These will be the next three elements of our levelTileMaps array.Â
-
Next, we'll create another array called backgroundMaps. As its name suggests, this will hold all the different backgrounds of the levels.
-
In this guide, we've imported backgrounds created in an external software such as Aseprite. If you haven't done so yet, check out our previous guide on how to import your own.
-
From the Scene tab, get out a set background image to ... block, place it right after set backgroundMaps to array of ...Â
-
Then get out a list get value at ... block. Change this to read backgroundMaps get value at num block.Â
-
We've also placed a set character position to x 25 y 88 block into the createLevel function, this will reset the player's position.Â
-
The block from on start have been repeated here: set tile ... to ... with wall ON blocks.Â
-
Finally, we'll still need to call the generateCoins function, generateItems function, and generateEnemies function to re-spawn these sprites every time a new level is created.