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 – adding functionalities to record the music level data


Carry out the following steps:

  1. First, we create a variable to toggle between the recording mode and normal playing mode. Open the html5games.audio.js file and add the code as follows:

      var audiogame = {
        isRecordMode : true,
        //existing code here
  2. Next, we add the following highlighted code in the keydown event handler. This code stores all our pressed keys in an array and prints them out to the console when the semicolon key is pressed:

    if (game.isRecordMode) {
      // print the stored music notes data when press ";" (186)
      if (e.which === 186) {
        var musicNotesString = "";
        for(var i=0, len=game.musicNotes.length; i<len; i++)     {
          musicNotesString += game.musicNotes[i].time + "," + game.musicNotes[i].line+";";
        }
        console.log(musicNotesString);
      }
    
      var currentTime = game.melody.currentTime.toFixed(3);
      var note = new MusicNote(currentTime, e.which-73);
      game.musicNotes.push(note);
    }
  3. Finally...