Time for action – displaying the countdown timer on the screen

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

  1. Ensure that the Score Management scene behavior is visible: click on the Dashboard tab, select Scene Behaviors, and double-click on the Score Management icon in the main panel.
  2. Click + Add Event | Basics | When Drawing.
  3. Double-click on the title of the new Drawing event, and rename it to Display Countdown.
  4. Click on the Drawing section button in the instruction block palette.
  5. Drag a draw text anything at ( x: 0 y: 0 ) block into the orange when drawing event block in the main panel.
  6. Enter the number 10 into the x: textbox and also enter 10 into the y: textbox.
  7. Click on the drop-down arrow in the textbox after draw text and select Text | Basics. Then click on the text & text block.
  8. In the first textbox in green, … & … block, enter the text COUNTDOWN: (all uppercase, followed by a colon).
  9. In the second textbox, after the & symbol, click on the drop-down arrow and select Basics, then click on the anything as text block.
  10. Click on the drop-down arrow in the … as text block, and select Number | Attributes | Countdown.
  11. Ensure that the new Display Countdown event looks like the following screenshot:
    Time for action – displaying the countdown timer on the screen
  12. Test the game.

What just happened?

When the game is played, we can now see in the upper-left corner of the screen, a countdown timer that represents the value of the Countdown attribute as it is decremented each second.

First, we created a new Drawing event, which we renamed to Display Countdown, and then we added a draw text anything at ( x: 0 y: 0 ) block, which is used to display the specified text in the required location on the screen.

We set both the x: and y: coordinates for displaying the drawn text to 10 pixels, that is, 10 pixels from the left-hand side of the screen, and 10 pixels from the top of the screen.

The next task was to add some text blocks that enabled us to display an appropriate message along with the value of the Countdown attribute. The text & text block enables us to concatenate, or join together, two separate pieces of text. The Countdown attribute is a number, so we used the anything as text block to convert the value of the Countdown attribute to text to ensure that it will be displayed correctly when the game is being played.

In practice, we could have just located the Countdown attribute block in the Attributes section of the palette, and then dragged it straight into the text & text block. However, it is best practice to correctly convert attributes to the appropriate type, as required by the instruction block. In our case, the number attribute is being converted to text because it is being used in the text concatenation instruction block. If we needed to use a text value in a calculation, we would convert it to a number using an anything as number block.

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

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