Fart Catcher

Level 2

With this step, you’re going to add a new level to the game that the player can get to by just pressing a button. Later, you can change your code to make it so they need a certain number of points, or something else, to get there.

Moving to the next level

First, create a new sprite as a button by either adding one from the library or drawing your own. I did a bit of both and came up with this:

The button sprite to switch levels

Now, the code for this button is clever: it’s designed so that every time you click it it will take you to the next level, no matter how many levels there are.

Add these scripts to your Button sprite. You will need to create some variables as you do so.

whenclickedsetmax-levelto2setmin-levelto1setcurrent-levelto1
whenthisspriteclickedchangecurrent-levelby1ifcurrent-level>max-levelthensetcurrent-leveltomin-levelbroadcastcollectable-cleanupbroadcastjoinlevel-current-level

Can you see how the program will use the variables you created?

  • max-level stores the highest level
  • min-level stores the lowest level
  • current-level stores the level the player is on right now

These all need to be set by the programmer (you!), so if you add a third level, don’t forget to change the value of max-level! min-level will never need to change, of course.

The broadcasts are used to tell the other sprites which level to display, and to clear up the collectables when a new level starts.

Make the sprites react

The Collectable sprite

Now you need to get the other sprites to respond to these broadcasts! Start with the easiest one: clearing all the collectables.

Add the following code to the Collectable sprite scripts to tell all its clones to hide when they receive the cleanup broadcast:

whenIreceivecollectable-cleanuphide

Since one of the first things any new clone does is show itself, you don’t have to worry about unhiding collectables!

The Platforms sprite

Now to switch the Platforms sprite. You can design your own new level later if you like, but for now let’s use the one I’ve already included — you’ll see why on the next step!

Add this code to the Platforms sprite:

whenIreceivelevel-1switchcostumetoLevel 1show
whenIreceivelevel-2switchcostumetoLevel 2show

It receives the joined messages of level- and current-level that the Button sprite sends out, and responds by changing the Platforms costume.

The Enemy sprite

In the Enemy sprite scripts, just make sure the sprite disappears when the player enters level 2, like this:

whenIreceivelevel-1show
whenIreceivelevel-2hide

If you prefer, you can make the enemy move to another platform instead. In that case, you would use a go to block instead of the show and hide blocks.

Make the Player Character appear in the right place

Whenever a new level starts, the Player Character sprite needs to go to the right place for that level. To make this happen, you need to change where the sprite gets its coordinates from when it first appears on the Stage. At the moment, there are fixed x and y values in its code.

Begin by creating variables for the starting coordinates: start-x and start-y. Then plug them into the go to block in the reset-character My blocks block instead of the fixed x and y values:

definereset-charactersetcan-jumptotruesetx-velocityto0sety-velocityto-0gotox:start-xy:start-y

Then for each broadcast announcing the start of a level, set the right start-x and start-y coordinates in response, and add a call to reset-character:

whenIreceivelevel-1setstart-xto-183setstart-yto42reset-character
whenIreceivelevel-2setstart-xto-218setstart-yto-143reset-character

Starting at Level 1

You also need to make sure that every time someone starts the game, the first level they play is level 1.

Go to the reset-game script and remove the call to reset-character from it. In its place, broadcast the min-level. The code you’ve already added with this card will then set up the correct starting coordinates for the Player Character sprite, and also call reset-character.

definereset-gamesetrotationstyleleft-rightsetjump-heightto15setgravityto2setx-speedto1sety-speedto1setlivesto3setpointsto0broadcastjoinlevel-min-level

Resetting the Player Character versus resetting the game

Notice that the first block in the Player Character sprite’s main green flag script is a call to the reset-game My blocks block.

This block sets up all the variables for a new game and then calls the reset-character My blocks block, which places the character back in its correct starting position.

Having the reset-character code in its own block separate from reset-game allows you to reset the character to different positions without having to reset the whole game.