Book Image

HTML5 Game Development by Example: Beginner's Guide

By : Seng Hin Mak
Book Image

HTML5 Game Development by Example: Beginner's Guide

By: Seng Hin Mak

Overview of this book

Table of Contents (18 chapters)
HTML5 Game Development by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Building a Physics Car Game with Box2D and Canvas
Index

Time for action – indicating a game over event in the console


Carry out the following steps:

  1. Open the audiogame.js JavaScript file.

  2. Add the following code in the jQuery ready function:

    $(audiogame.melody).bind('ended', onMelodyEnded);
  3. Add the following event handler function at the end of the file:

      // show game over scene on melody ended.
      function onMelodyEnded() {
        console.log('song ended');
        alert ('success percent: ' + audiogame.totalSuccessCount / audiogame.totalDotsCount * 100 + '%');
      }
    })(jQuery);
  4. It is time to save all files and play the game in a web browser. When the game is over, we should see a pop-up alert with the successful rate.

What just happened?

We just listened to the ended event of the audio element and handled it with a handler function.

Handling audio events

There are many other events in the audio element. The following table lists a few commonly used audio events:

Event

Discussion

ended

Sent when the audio element finishes a playback

play

Sent when the...