Skip to content

Sounds

Sounds stores named sound pools and provides simple playback controls. Game loads the configured sound definitions and exposes the shared service as services.sounds.

const sounds = this.game.sounds;
sounds.play('confirm');
sounds.pause('background-music');

Define sounds when creating Game:

const soundDefinitions = [
{ name: 'confirm', path: '/sounds/confirm.wav', size: 3, volume: 0.6 },
{
name: 'background-music',
path: '/sounds/music.mp3',
size: 1,
volume: 0.35,
loop: true,
},
];
flowchart LR
	A[SoundDefinition] --> B[Sounds.load]
	B --> C[SoundPool]
	C --> D[Reusable Audio elements]
	E[Sounds.play name] --> C
	C --> F[Try current pool entry then rotate]
sounds.load(soundDefinitions);

Creates one SoundPool for each definition and stores it under definition.name.

Definition field Meaning
name Stable key used by get(), play(), and pause().
path URL of the audio file.
size Number of reusable audio elements in the pool. Use a larger number when the same effect may overlap itself.
volume Per-sound volume from 0 (silent) to 1 (full volume).
loop Optional; when true, each pooled audio element loops. Defaults to false.
const pool = sounds.get('confirm');

Returns the named SoundPool. It throws Unknown sound: <name> if no matching definition was loaded.

sounds.play('confirm');

Finds the named pool and tries its current audio element, then rotates to the next element. A pool can therefore let short effects overlap instead of restarting one shared audio element.

sounds.pause('background-music');

Pauses the pool’s current audio element. It does not remove the definition or reset the whole service.

sounds is a Record<string, SoundPool> of loaded pools. Treat it as a lookup owned by the service; use get(), play(), and pause() rather than changing it directly.