Time for action – listening for the countdown to reach zero

The game file that needs to be imported and loaded for this session is 5961_04_15.stencyl.

Rather than allowing the countdown to continue beyond zero and into negative numbers, in the following steps we're going to create a behavior with an event that will react when the player runs out of time:

  1. Create a new Actor Behavior called Health.
  2. Add a new Time event, Every N Seconds, and rename it to Countdown expired.
  3. Change the do every … seconds textbox to 1 second.
  4. Click on the Flow section button in the palette.
  5. Ensure that the Conditions category button is selected.
  6. Drag the orange if block into the do every 1 seconds event.
  7. Click on the drop-down arrow in the if block, select Comparison, and, in the Inequality section, click on the 0 < 0 block.
  8. In the right-hand textbox of the green 0 < 0 block, enter the number 1.
  9. In the block palette, click on the Behavior section button and the Attributes category button.
  10. In the For Scene subcategory of the block palette, locate the red for this scene, get text from behavior text block and drag it into the left-hand textbox in the 0 < 0 block.
  11. Click on the drop-down arrow in the left-hand textbox of the for this scene, get … from behavior … block and select Attribute Names in the drop-down menu.
  12. In the pop-up menu, move the mouse over the letter S, then select Score Management and then Countdown, as shown in the following screenshot:
    Time for action – listening for the countdown to reach zero
  13. Locate the kill Self block in the palette, and drag it into the if for this scene, get countdown from behavior Score Management block, as shown in the following screenshot:
    Time for action – listening for the countdown to reach zero
  14. Attach the behavior to the monkey actor.
  15. Test the game.
  16. When the game is being displayed on the screen, examine the contents of the Log Viewer window. Watch what happens to the monkey after 10 seconds.
  17. Close the game and close the Log Viewer window.

What just happened?

We've attached the newly created Health behavior to the monkey and we should now be familiar with the main event block in this behavior—it will do something every one second, because we created a do every … event block.

For the first time, we are introducing an if … block, which allows the instruction blocks to make a decision based on some information in our game. We want to know when our countdown timer is less than one, or in other words, when our player is out of time!

When we use an if … block, we must give it something to test and, in this case, we are comparing two numbers using the green less than block. The < symbol means less than and we're checking to see if something is less than the digit 1, which we entered into the second textbox.

The something that we're checking is decided by the red for this scene, get _Countdown from behavior Score Management block shown in the following screenshot, which, at first, might look a little complicated. So, let's break it down:

What just happened?

This block will retrieve the value of an attribute that can be found in another behavior in the current scene. We used a very useful shortcut to populate the textboxes. We located the name of the attribute that we needed by using the drop-down menus. We could have typed the information into the boxes, and that works perfectly well—if we type all the information correctly! If we were to type any of the information incorrectly, then the instruction just won't work. So, why not just use the shortcut?

In plain English, our red for this scene, get … block could be phrased as follows:

In the current scene, retrieve the value of the Countdown attribute that is used in the Score Management behavior.

We created the Score Management behavior in the Time for action – listening for the countdown to reach zero section,and we know that that there is an attribute called Countdown, which is decreased every second.

If we now look at our new Countdown expired event as a whole, we can phrase it in English as:

Every one second, check if the value of Countdown is less than one and, if it is, kill Self.

Because we attached this behavior to the monkey actor, Self refers to the monkey.

We have a timer that will count down second-by-second, and we now have an event that checks when the value of the countdown is less than one and, if the countdown is less than one, the monkey is killed!

As we discovered earlier, a kill block is somewhat abrupt, and our monkey actor just disappears without warning. We'll be giving our game some polish later on but, remember, for the time being, we're just getting our gameplay mechanics working correctly.

Note

There is a very important point to note when retrieving attribute values using the red attribute blocks. Note that the name of the attribute—in our case it is Countdown—is preceded with an underscore character so it appears as _Countdown. The underscore character is needed because these instruction blocks use Stencyl's internal names for the attributes, not the friendly names that we have given to them. For this reason, it is generally recommended, as we have done, to use the pop-up menus to populate the textboxes in these blocks.

What if? Otherwise…

We have used an if ... block to check for a specific condition, and to carry out actions if that condition is True. In our game, the actor is killed when the Countdown attribute has a value less than one.

However, what can we do if we need to respond when the condition is not True?

In the Flow section of the palette, in the Conditions category, there are two other instruction blocks alongside the if … block in the Conditionals subcategory. Both of these alternative conditional blocks begin with the word otherwise.

Research what these blocks do, and consider how they might be useful when creating behaviors for a game.

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

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