Time for action – decrementing the number of lives

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

  1. If the Game Settings dialog box is still open, click on OK to close it.
  2. Open the Manage Player Collisions actor behavior.
  3. Click on the Collides with Enemies event in the left-hand panel.
  4. Click on the Attributes section button in the palette.
  5. Click on the Game Attributes category button.
  6. Locate the purple set Lives to 0 block under the Number Setters subcategory and drag it into the orange when event so that it appears above the red trigger event RestartLevel in behavior Health for Self block.
  7. Click on the drop-down arrow in the set Lives to … block and select 0 - 0 in the Math section.
  8. In the left textbox of the … - … block, click on the drop-down arrow and select Game Attributes | Lives.
  9. In the right-hand textbox, enter the digit 1.
  10. Locate the print anything block in the Flow section of the palette, under the Debug category, and drag it below the set Lives to Lives – 1 block.
  11. In the print … block, click on the drop-down arrow and select Text | Basics | text & text.
  12. In the first empty textbox, type Lives remaining: (including the colon).
  13. Click on the drop-down arrow in the second textbox and select Basics | anything as text.
  14. In the … as text block, click on the drop-down arrow and select Number | Game Attributes | Lives.
  15. Ensure that the Collides with Enemies event looks like the following screenshot:
    Time for action – decrementing the number of lives
  16. Test the game; make the monkey collide with an enemy actor, such as the croc, and watch the debug console!

What just happened?

We have modified the Collides with Enemies event in the Manage Player Collisions behavior so that it decrements the number of lives by one when the monkey collides with an enemy actor, and the new value of Lives is shown in the debug console.

This was achieved by using the purple game attribute setter and getter blocks to set the value of the Lives game attribute to its current value minus one. For example, if the value of Lives is 3 when the event occurs, Lives will be set to 3 minus 1, which is 2!

The print … block was then used to display a message in the console, advising how many lives the player has remaining. We used the text & text block to join the text Lives remaining: together with the current value of the Lives game attribute. The anything as text block converts the numeric value of Lives to text to ensure that it will display correctly.

Currently, the value of the Lives attribute will continue to decrease below 0, and the monkey will always be repositioned at its starting point. So our next task is to make something happen when the value of the Lives game attribute reaches 0!

No more click-by-click steps!

From this point onwards, click-by-click steps to modify behaviors and to locate and place each instruction block will not be specified! Instead, an overview of the steps will be provided, and a screenshot of the completed event will be shown towards the end of each Time for action section.

The search facility, at the top of the instruction block palette, can be used to locate the required instruction block; simply click on the search box and type any part of the text that appears in the required block, then press the Enter key on the keyboard to display all the matching blocks in the block palette.

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

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