| function playSound(sound) { |
|
|
| if (typeof isSoundOn === 'undefined' ? false : isSoundOn) { |
| let soundToPlay; |
|
|
| |
| switch (sound) { |
| case 'clownSound1': |
| soundToPlay = clownSound1Snd; |
| break; |
| case 'clownSound2': |
| soundToPlay = clownSound2Snd; |
| break; |
| case 'clownSeesYou': |
| soundToPlay = clownSeesYouSnd; |
| break; |
| case 'gfMove': |
| soundToPlay = gfMoveSnd; |
| break; |
| case 'lose': |
| soundToPlay = loseSnd; |
| break; |
| case 'unlockDoor': |
| soundToPlay = unlockDoorSnd; |
| break; |
| case 'useKnife': |
| soundToPlay = useKnifeSnd; |
| break; |
| case 'message': |
| soundToPlay = messageSnd; |
| break; |
| case 'success': |
| soundToPlay = successSnd; |
| break; |
| } |
|
|
| |
| if (soundToPlay && soundToPlay.isLoaded()) { |
| soundToPlay.setLoop(false); |
| soundToPlay.playMode('restart'); |
| if (soundToPlay.isPlaying()) { |
| soundToPlay.stop(); |
| } |
| soundToPlay.play(); |
| } else { |
| console.log('Sound file is not loaded or undefined.'); |
| } |
| } |
| } |
|
|
|
|
|
|
| function toggleSound() { |
| isSoundOn = !isSoundOn; |
| localStorage.setItem("isSoundOn", isSoundOn.toString()); |
|
|
| if (isSoundOn) { |
| if (bgMusic.currentTime < 10) { |
| bgMusic.currentTime = 10; |
| } |
| bgMusic.play(); |
| |
| } else { |
| bgMusic.pause(); |
| } |
| updateSoundIcon(); |
| } |
|
|
| |
| if (localStorage.getItem("isSoundOn") === null) { |
| toggleSound(); |
| } |
|
|
|
|
| function updateSoundIcon() { |
| soundIcon.src = isSoundOn ? "/static/assets/img/sondon.png" : "/static/assets/img/soundoff.png"; |
| } |
|
|
| function playMessageSound() { |
| try { |
| const messageSound = document.getElementById("messageSound"); |
| if (messageSound) { |
| messageSound.currentTime = 0; |
| messageSound.volume = 1.0; |
| messageSound.play().catch((error) => { |
| console.log("Could not play message sound:", error); |
| }); |
| } |
| } catch (error) { |
| console.error("Error playing message sound:", error); |
| } |
| } |