Time for action – detecting when Lives reaches zero

The game file to import and load for this session is 5961_06_05.stencyl.

  1. Create a new scene called Game Over— Select the Dashboard tab, select Scenes, and then select Click here to create a new Scene. Leave all the settings at their default configuration and click on OK.
  2. Close the tab for the newly created scene.
  3. Open the Manage Player Collisions behavior and click on the Collides with Enemies event to display the event's instruction blocks.
  4. Insert a new if block under the existing print block.
  5. Modify the if block to if Lives > 0.
  6. Move the existing block, trigger event RestartLevel in behavior Health for Self into the if Lives > 0 block.
  7. Insert an otherwise block below the if Lives > 0 block.
  8. Insert a switch to Scene and Crossfade for 0 secs block inside the otherwise block.
  9. Click on the Scene option in the new block, then click on Choose Scene and select the Game Over scene.
  10. Change the secs textbox to 0 (zero).
  11. Ensure that our modified Collides with Enemies event now looks like the following screenshot:
    Time for action – detecting when Lives reaches zero
  12. Test the game; make the monkey run into an enemy actor, such as the croc, three times.

What just happened?

We have modified the Collides with Enemies event so that the value of the Lives game attribute is tested after it has been decremented, and the game will switch to the Game Over scene if the number of lives remaining is less than zero.

If the value of Lives is greater than zero, the RestartLevel event in the monkey's Health behavior is triggered. However, if the value of Lives is not greater than zero, the instruction in the otherwise block will be executed, and this switches to the (currently blank) Game Over scene that we have created.

If we review all the instructions in the completed Collides with Enemies event, and write them in English, the statement will be:

When the monkey collides with an enemy, reduce the value of Lives by one and print the new value to the debug console. Then, if the value of Lives is more than zero, trigger the RestartLevel event in the monkey's Health behavior, otherwise switch to the Game Over scene.

Before continuing, we should note that the Game Over scene has been created as a temporary measure to ensure that as we are in the process of developing the game, it's immediately clear to us (the developer) that the monkey has run out of lives. In Chapter 7, Polishing the Game, we'll be introducing a more appropriate and visually appealing game over message for our players!

Have a go hero

The game file to import and load for this session is 5961_06_06.stencyl.

  • Change the Countdown attribute value to 30— open the Jungle scene, click on the Behaviors button, then select the Score Management behavior in the left panel to see the attributes for this behavior.

Note

The following tasks in this Have a go hero session are optional — failure to attempt them will not affect future tutorials, but it is a great opportunity to put some of our newly learned skills to practice!

In the section, Time for action – displaying the countdown timer on the screen, we learned how to display the value of the countdown timer on the screen during gameplay.

Using the skills that we have acquired in this chapter, try to complete the following tasks:

  • Update the Score Management behavior to display the number of lives at the upper-right corner of the screen, by adding some new instruction blocks to the Display Counter event.
  • Rename the Display Counter event to Display HUD.
  • Remove the print Countdown block from the Decrement Countdown event also found in the Score Management behavior.

    Tip

    Right-click on the instruction block and review the options available in the pop-up menu!

  • Remove the print Lives remaining: & Lives as text instruction block from the Collides with Enemies event in the Manage Player Collisions behavior.

    Note

    The suggested modifications along with comments can be viewed in the downloadable files that accompany this book, in the file named 5961_06_07.stencyl.

Removing debug instructions

Why did we remove the debug print … blocks in the previous Have a go hero session? Originally, we added the debug blocks to assist us in monitoring the values of the Countdown attribute and Lives game attribute during the development process. Now that we have updated the game to display the required information on the screen, the debug blocks are redundant!

While it would not necessarily cause a problem to leave the debug blocks where they are, it is best practice to remove any instruction blocks that are no longer in use. Also, during development, excessive use of debug print blocks can have an impact on the performance of the game; so it's a good idea to remove them as soon as is practical.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.20.236