Time for action – implementing a jumping sound effect

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

  1. Before we create the behavior to manage the sound effects, let's remove the redundant Game Over scene from the game (right-click on the scene's thumbnail in the Scenes panel and select Remove).
  2. Download Stencyl Book Jump Effect from StencylForge and close the tabs for the jump sound effect and StencylForge.
  3. Create a new scene behavior called Sound Management.
  4. Click on the Attach to Scene button, and select Level1.
  5. Close the tab for Level1 to return to the Sound Management behavior.
  6. Attach the behavior to all the other scenes in our game, including the Main Menu scene.
  7. Close the tabs for all the open scenes and return to the Sound Management behavior.
  8. Add a new custom event called Jump SFX and configure the event as shown in the following screenshot, ensuring to set the event's trigger name to JumpSFX:
    Time for action – implementing a jumping sound effect
  9. Open the actor behavior named Jump and Run Movement, add a new keyboard input event (+ Add Event | Input | Keyboard), rename it to Trigger Jump SFX, and configure it as shown in the following screenshot, ensuring that Control is set to up. Note that the blue Jumping block is an attribute that is used in the Jump and Run Movement behavior (Attributes | Getters in the palette):
    Time for action – implementing a jumping sound effect
  10. Test the game; make the monkey jump!

What just happened?

We've just added a jumping sound effect to the game!

The very first task was to remove the Game Over scene, because our game no longer uses it. Then, after downloading the sound effect from StencylForge, we created a new scene behavior called Sound Management, and attached it to all the scenes in our game; we may need to have sounds playing in scenes other than the playable levels.

Tip

To check which scenes use a specific behavior, right-click on the behavior's thumbnail in the Scene Behaviors panel in the Dashboard tab, and select Who uses this? from the pop-up menu. This feature is also available in the Actor Behaviors panel.

We then created a new custom event called Jump SFX in the Sound Management scene behavior, which simply plays the specified sound effect on channel 1 when it is triggered. Further information about sounds and channels can be found in the following section entitled, Examining the play sound instruction blocks.

Our next step was to open the Jump and Run Movement behavior and add a new keyboard event. This event called Trigger Jump SFX waits for the up control to be pressed, and when that happens, it triggers the JumpSFX custom event in the Sound Management behavior. However, we placed the trigger inside an instruction that checks if not Jumping; this is shorthand for if Jumping is not = true, and is just a different way of carrying out the same test.

Why are we checking to see if the Jumping attribute in the Jump and Run Movement behavior is not true? Because we didn't create this behavior (it is one of the ready-made behaviors that is provided with Stencyl), a little research was required to find out how the jumping routines worked! It isn't necessary to fully understand the behavior in detail, but if we examine the main always (when updating) event in the Jump and Run Movement behavior, we can see that there is an attribute called Jumping that is set when (surprise!) the actor is jumping. We can also see attributes called Jump and On Ground, as shown in the following screenshot, which is an excerpt from the Jump and Run Movement behavior:

What just happened?

With this information, we might surmise that the Jumping attribute is set to true when the actor is jumping, and that it is set to false when the actor is no longer jumping.

We only want the jumping sound effect to be played when the player presses the up control and the actor is not already jumping, otherwise repeated presses of the up control will cause the sound effect to play, even when the monkey is already in the air, and that will just be annoying for players of our game!

Testing the game demonstrates that, however many times the player presses the up control, the jumping sound effect will only be played if the monkey is not already jumping. Perfect!

Have a go hero

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

Currently, the jumping sound effect will still play when the game is paused and the player presses the up control.

  • Modify the Jump SFX custom event, so the sound effect will not occur when the game is paused

A suggested solution is provided in the download file 5961_08_03.stencyl, along with comments in the Jump SFX event of the Sound Management scene behavior.

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

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