Book Image

HTML5 Game Development Hotshot

By : Seng Hin Mak, Makzan Makzan (Mak Seng Hin)
Book Image

HTML5 Game Development Hotshot

By: Seng Hin Mak, Makzan Makzan (Mak Seng Hin)

Overview of this book

Table of Contents (15 chapters)
HTML5 Game Development HOTSHOT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding sound effects


In this task, we will add sound effects to the game.

Prepare for lift off

We want to ensure that the SoundJS library is ready in the vendors folder. If the file is missing, we can find it in the CreateJS website or the code bundle.

We also need to have the following sound effects files prepared in the audio folder. There are sound effects to start the game, click on a button, trash sushi, and earn money.

Engage thrusters

In the following steps, we will add sound effects to our game:

  1. Let's begin by loading the audio file. We add the following sound-loading code into the game.js file:

    game.load = function() {
      // begin loading content (only sounds to load)
      var assetsPath = "audio/";
    manifest = [
          {id:"button", src:"button.ogg"},
          {id:"refill", src:"refill.ogg"},
          {id:"earn-money", src:"earn money.ogg"},
          {id:"start-game", src:"start game.ogg"}
      ];
      cjs.Sound.alternateExtensions = ['aif', 'webm'];
      preload = new cjs.LoadQueue(true, assetsPath);
      preload...