From c2c457a95074fd4044d1ae1ab8d2e6854b2e2e6d Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Sun, 9 Jun 2024 16:56:28 +0200 Subject: [PATCH] sound fix --- src/lib/game/game.ts | 2 ++ src/lib/game/utils/audioManager.ts | 8 +++----- src/routes/+page.svelte | 25 ++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/lib/game/game.ts b/src/lib/game/game.ts index 21a16704..ec2fff3c 100644 --- a/src/lib/game/game.ts +++ b/src/lib/game/game.ts @@ -79,6 +79,8 @@ export class Game extends Container { } public async play() { + this.audio.isEnabled = true; + // setInterval(() => { // console.log("FPS", this.app.ticker.FPS) // console.log("Objects", this.children.length) diff --git a/src/lib/game/utils/audioManager.ts b/src/lib/game/utils/audioManager.ts index 68f4b998..0deed245 100644 --- a/src/lib/game/utils/audioManager.ts +++ b/src/lib/game/utils/audioManager.ts @@ -20,6 +20,8 @@ type SoundName = | "YEAH" export class AudioManager { + public isEnabled = false + private chop1 = new Howl({ src: chop1Audio, }) @@ -80,7 +82,7 @@ export class AudioManager { } private play(audios: Howl[]) { - if (!audios.length) { + if (!audios.length || !this.isEnabled) { return } @@ -90,10 +92,6 @@ export class AudioManager { } randomAudio.play() - - randomAudio.once("playerror", () => { - randomAudio.stop() - }) } public playSound(name: SoundName) { diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 6fba8564..f59fb44f 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -6,14 +6,18 @@ export let data: PageServerData + let game = new Game() let gameElement: HTMLElement + let showSoundSwitch = true - onMount(() => { - let game = new Game() + const handleSoundSwitchClick = () => { + showSoundSwitch = !showSoundSwitch + game.play() + } + onMount(() => { const initGame = async () => { await game.init() - void game.play() gameElement?.appendChild(game.app.canvas) game.app.resizeTo = gameElement @@ -43,6 +47,9 @@
+
+ +
@@ -57,13 +64,25 @@ #game-canvas { width: 100%; height: 28em; + touch-action: none; } .game-block { + position: relative; width: 100%; padding: 4em 0; } + .game-block .buttons-block { + margin-top: 1em; + display: flex; + justify-content: center; + } + + .buttons-block .sound-switch { + background: var(--color-background); + } + section { text-align: center; padding: 2em 1em;