Better sound handling
This commit is contained in:
parent
fd1e0447a4
commit
07424dd662
10
main.js
10
main.js
|
@ -1,9 +1,9 @@
|
||||||
const sounds = {
|
const sounds = {
|
||||||
hitBlock: new Audio('assets/hit_block.wav'),
|
hitBlock: new SoundManager('assets/hit_block.wav'),
|
||||||
hitCoin: new Audio('assets/hit_coin.wav'),
|
hitCoin: new SoundManager('assets/hit_coin.wav'),
|
||||||
hitBall: new Audio('assets/hit_ball.wav'),
|
hitBall: new SoundManager('assets/hit_ball.wav'),
|
||||||
roundOver: new Audio('assets/round_success.wav'),
|
roundOver: new SoundManager('assets/round_success.wav'),
|
||||||
gameOver: new Audio('assets/game_over.wav')
|
gameOver: new SoundManager('assets/game_over.wav'),
|
||||||
};
|
};
|
||||||
|
|
||||||
let coinCount = 0;
|
let coinCount = 0;
|
||||||
|
|
19
sound-manager.js
Normal file
19
sound-manager.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
class SoundManager {
|
||||||
|
constructor(soundSource) {
|
||||||
|
this.soundSource = soundSource;
|
||||||
|
this.sound = new Audio(this.soundSource);
|
||||||
|
this.lastPlayed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
play() {
|
||||||
|
if (Date.now() - this.lastPlayed < 50)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const node = this.sound.cloneNode();
|
||||||
|
node.play();
|
||||||
|
setTimeout(() => {
|
||||||
|
node.remove();
|
||||||
|
}, this.sound.duration * 1000);
|
||||||
|
this.lastPlayed = Date.now();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user