Time for action – switching animations with instruction blocks

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

  1. Ensure that the Health actor behavior is displayed on the screen — click on the Dashboard tab, then click on Actor Behaviors, and double-click on the Health behavior's icon in the main panel.
  2. Click on the + Add Event button at the top of the left-hand panel.
  3. Select Advanced, and then Custom Event.
  4. Rename the new Custom Event to Show Angel in the left-panel.
  5. In the new event's when … happens block, enter the text ShowAngel (note that there is no space).
  6. In the instruction block palette, click on the Behavior section button, then click on the State category button.
  7. Drag the Enable behavior text for Self instruction block into the orange when ShowAngel happens block.
  8. In the new Enable behavior ... for Self block, click on Enable and change it to Disable.
  9. In the same block, click on the drop-down arrow after the word behavior, select Behavior Names in the pop-up menu, then select J | Jump and Run Movement.
  10. Drag another Enable behavior text for Self instruction block from the palette and drop it below the one that we just added.
  11. Change Enable to Disableand using the drop-down menu, change the empty textbox to Manage Player Collisions.
  12. Click on the Actor section button in the palette, and then click on the Draw category button.
  13. Locate the switch animation to anim attribute for Self block, and drag it beneath the Disable behavior Manage Player Collisions for Self block.
  14. In the switch animation to block, type Angel in the text box.
  15. In the palette, under Scene | Game Flow, locate the Fade Out for 0 secs, then reload and Fade In for 0 secs block and drag it underneath the switch animation to Angel for Self block. Be certain to select the correct block from the palette in this step; there is another similarly-named block in the same subcategory!
  16. In the red block that we just added, enter the number 1 in the text box after the words Fade out for.
  17. Enter the number 1 in the text box after the words Fade in for.
  18. Ensure that the new Show Angel custom event looks like the following screenshot:
    Time for action – switching animations with instruction blocks
  19. In the left-hand panel, click on the title of the Countdown expired event to display the do every 1 seconds instructions.
  20. Right-click on the kill Self block and select Remove from the drop-down menu.
  21. In the Behavior | Triggers section of the palette, locate the trigger event text in behavior text for Self block, and drag it into the orange if block.
  22. Enter the text ShowAngel (without spaces) in the textbox after the words trigger event.
  23. Click on the drop-down arrow in the textbox after the word behavior, and select Behavior Names | H | Health from the pop-up menu.
  24. Ensure that the updated Countdown expired event looks like the following screenshot:
    Time for action – switching animations with instruction blocks
  25. Test the game. Wait for the countdown to reach zero while watching the monkey actor.

What just happened?

We have created a new custom event that switches to the monkey's Angel animation, and is triggered when the countdown timer expires.

Our new custom event utilizes two new instruction blocks that we haven't used previously in our game. The first block that we introduced, in the Show Angel event, disables the Jump and Run Movement behavior, and we need to do this for two reasons: firstly, to prevent the player from continuing to control the monkey after the countdown has completed, and secondly, because the Jump and Run Movement behavior continuously updates the monkey actor. If we failed to disable the movement behavior, it would automatically switch from the Angel animation, straight back to one of the monkey's idle animations, before we even had time to see the Angel animation onscreen! We also added a block to disable the Manage Player Collisions behavior. When the countdown expires, we don't want the player to be able to collect any items, which could happen if the monkey was falling down towards one of the collectibles just as the countdown expired. Disabling this behavior will also prevent the monkey switching to the Angel animation if it falls onto an enemy at the same moment the countdown expires.

Note that we don't need to reenable the behaviors at any time, because we are reloading the scene, which puts the behaviors back into their original enabled state.

Beneath the blocks to disable the behaviors, we introduced a switch animation to … for Self block. This block allows us to specify which animation we want to display, and we must supply the exact name of the animation, as it appears in the Animation Editor, including any capitalization and spaces.

The last step in creating our custom event was to add the instruction to fade out the scene and reload it. Reloading a scene puts it back to how it was right at the start of the game – it is completely reset as though it has never been played. This is the desired behavior for our game – when the countdown expires, the player must restart the scene. Don't worry; we'll be increasing the countdown to a more achievable value, later in the development process!

As we learned in Chapter 4, Creating Behaviors, custom events must be triggered so they can carry out the required actions. We placed the trigger instruction into the existing Countdown expired event that checks every one second to determine if the countdown has reached zero. Originally, when the countdown reached zero, we simply killed the monkey, so we had to remove the kill Self block before inserting the trigger event... instruction.

Have a go hero

We have used the switch animation to instruction, which we found in the Animation subcategory of the Actor | Draw section of the palette.

There are several other instruction blocks in the Animation subcategory that can be used in behaviors to find information about and control animations associated with a particular actor.

Review these instruction blocks and consider how they might be useful when developing a game, remembering that right-clicking on any block and selecting View | Help will display some useful information.

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

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