Better sound handling
This commit is contained in:
		
							
								
								
									
										10
									
								
								main.js
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								main.js
									
									
									
									
									
								
							| @@ -1,9 +1,9 @@ | ||||
| const sounds = { | ||||
|     hitBlock: new Audio('assets/hit_block.wav'), | ||||
|     hitCoin: new Audio('assets/hit_coin.wav'), | ||||
|     hitBall: new Audio('assets/hit_ball.wav'), | ||||
|     roundOver: new Audio('assets/round_success.wav'), | ||||
|     gameOver: new Audio('assets/game_over.wav') | ||||
|     hitBlock: new SoundManager('assets/hit_block.wav'), | ||||
|     hitCoin: new SoundManager('assets/hit_coin.wav'), | ||||
|     hitBall: new SoundManager('assets/hit_ball.wav'), | ||||
|     roundOver: new SoundManager('assets/round_success.wav'), | ||||
|     gameOver: new SoundManager('assets/game_over.wav'), | ||||
| }; | ||||
|  | ||||
| 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(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user