Adding obstacles and switching levels
We need two more colours. One for obstacles that we can't pass through from left to right, and one for the door to the next level.
Choose your colours and paint them on the stage background. I've used a slightly different shade of green for the obstacle, and yellow for the door. Notice that I've added a streak of lava to the middle of the left-right obstacle, just to prevent cheating. You'll see why below. |
Coding the left-right obstacle
The simplest way to do this is to make the hero bounce. We do this by simply reversing the xspeed if the hero touches the obstacle colour.
To reverse direction, simply multiply the xspeed by -1 It's not perfect however, you'll find that if you're persistent and hold down the arrow keys, you can push slowly through the obstacle. That's what the lava middle is for! |
Switching levels
This requires a bit of planning. The stage is responsible for handling the levels, each level is just a new costume of the stage. The hero has to tell the stage to change levels when he touches the door colour.
We do that by broadcasting a message. That message is a way of communicating between two objects in the game. Firstly we set up the stage. Create a second costume with a new level on it. Then add a block starting with - 'When I receive ..' and in the drop-down box, choose 'New message'. Call the new message NewLevel and make it select the next backdrop. Make sure that when the game starts you start on the first level by switching the backdrop back to the first costume when the green flag is clicked. |
Now you are ready to add the code to send the message. Select the Hero sprite again and add another colour detection block that will look for the doorway colour and send your new message.
You'll need to move the hero to the right starting position, so that it looks right. It's best to have a standard starting position for each level. |