window.ontouchmove = function(evt) { evt.preventDefault(); } var currentSoundtrack = -1; function playSoundtrack(tracks) { console.log("Playing track..."); var keys = Object.keys(tracks); if (currentSoundtrack === -1) currentSoundtrack = randInt(0, keys.length); var key = keys[currentSoundtrack]; var track = tracks[key]; console.log("Playing track", key); var trackId = currentSoundtrack; while (currentSoundtrack === trackId) currentSoundtrack = randInt(0, keys.length); if (track.ready) { track.play(); track.once("ended", function() { playSoundtrack(tracks) }); console.log("playing immediately, as it's loaded"); } else { console.log("loading it..."); track.once("load", function() { console.log("loaded."); track.play(); track.once("ended", function() { playSoundtrack(tracks) }); }); } } playSoundtrack(assets.soundtracks); function run() { var game = new Game(document.getElementById("canvas")); game.canvas.width = window.innerWidth; game.canvas.height = window.innerHeight; var worldgen = new WorldGen(game); game.start(worldgen); game.onstop = function(score) { alert("You lost! Score: "+score); run(); } } run();