Time for action – adding functionalities to record the music level data
Carry out the following steps:
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
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); }
Finally...