From ecc12b2ef3b058ddd8bf9612afd7c9276f0386f9 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Tue, 14 Aug 2018 11:05:32 +0530 Subject: * removed unwanted dependencies * improved compression * Introduces Audio Player :sparkles: :fire: :racehorse: :snowflake: :gem: --- src/js/AudioPlayer.js | 25 +++++++++++++++++++++++++ src/js/Game.js | 21 +++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/js/AudioPlayer.js (limited to 'src') diff --git a/src/js/AudioPlayer.js b/src/js/AudioPlayer.js new file mode 100644 index 0000000..952f487 --- /dev/null +++ b/src/js/AudioPlayer.js @@ -0,0 +1,25 @@ +export default class AudioPlayer{ + constructor(){ + this.audioPlayer = new Audio(); + this.url = window.URL || window.webkitURL; + this.soundURL = null; + this.attachEventListeners(); + } + + attachEventListeners(){ + this.audioPlayer.addEventListener('error', (e)=> { + console.log("Error: " + e); + }, false); + this.audioPlayer.addEventListener('ended', (e)=> { + this.audioPlayer = null; + this.url.revokeObjectURL(this.soundURL); + }, false); + } + + play(soundUrl){ + this.soundURL = soundUrl; + this.audioPlayer.src = this.soundURL; + this.audioPlayer.play(); + + } +} \ No newline at end of file diff --git a/src/js/Game.js b/src/js/Game.js index 01b5356..a419496 100644 --- a/src/js/Game.js +++ b/src/js/Game.js @@ -1,9 +1,12 @@ + +import AudioPlayer from "./AudioPlayer"; + export default class Game{ constructor(){ - this.url = window.URL || window.webkitURL; + this.soundUrl = jsfxr([3,,0.3469,0.6652,0.2097,0.0671,,0.0916,,,,0.3062,0.8509,,,0.5633,0.0985,-0.0068,1,,,,,0.5]); this.player = new Audio(); - this.player.on + console.log(kontra); this.init(); } @@ -16,7 +19,7 @@ export default class Game{ let sprite = kontra.sprite({ x: 100, // starting x,y position of the sprite y: 80, - color: 'blue', // fill color of the sprite rectangle + color: 'white', // fill color of the sprite rectangle width: 20, // width and height of the sprite rectangle height: 40, dx: 2 // move the sprite 2px to the right every frame @@ -33,17 +36,7 @@ export default class Game{ } if(kontra.keys.pressed('space')){ - - this.player.addEventListener('error', (e)=> { - console.log("Error: " + player.error.code); - }, false); - this.player.addEventListener('ended', (e)=> { - this.url.revokeObjectURL(this.soundURL); - }, false); - this.player.pause(); - this.player.src = this.soundUrl; - let play = this.player.play(); - console.log(play) + (new AudioPlayer()).play(this.soundUrl); } }, render: ()=> { // render the game state -- cgit v1.2.3