Phaser 3 Web Audio SFX & sprite sync workflow
1. Guard browser Web Audio unlock cleanly
Chromium, Safari, and Firefox keep new AudioContext instances suspended until the first pointer or key gesture. Phaser 3 exposes this.sound.locked—if you fire menu or title-screen sounds while locked without guarding them, Phaser queues and blasts them simultaneously on first click. Defer loops to UNLOCKED and drop pre-gesture one-shots:
export class SfxController {
constructor(scene) {
this.scene = scene;
this.lastPlayed = new Map();
this.comboStep = 0;
this.lastCoinAt = 0;
}
startLoop(key, volume = 0.55) {
const snd = this.scene.sound.add(key, { loop: true, volume });
if (this.scene.sound.locked) {
this.scene.sound.once(Phaser.Sound.Events.UNLOCKED, () => snd.play());
} else {
snd.play();
}
return snd;
}
playOneShot(key, { cooldownMs = 35, detuneRange = 45, volume = 0.85 } = {}) {
if (this.scene.sound.locked) return;
const now = this.scene.time.now;
if (now - (this.lastPlayed.get(key) ?? -9999) < cooldownMs) return;
this.lastPlayed.set(key, now);
this.scene.sound.play(key, {
volume,
detune: Phaser.Math.Between(-detuneRange, detuneRange),
});
}
}
2. Pentatonic detune escalation & sprite frame sync
Phaser’s Web Audio sound manager accepts detune in cents (100 cents = 1 semitone). Step rapid coin or gem pickups paired with Coins & pickups animations across pentatonic offsets whenever pickups occur within 450 ms:
const PENTATONIC_CENTS = [0, 200, 400, 700, 900, 1200];
playPickupStreak(key = 'coin') {
const now = this.scene.time.now;
this.comboStep = now - this.lastCoinAt < 450
? Math.min(this.comboStep + 1, PENTATONIC_CENTS.length - 1)
: 0;
this.lastCoinAt = now;
this.scene.sound.play(key, {
volume: 0.85,
detune: PENTATONIC_CENTS[this.comboStep],
});
}
3. Load manifest.json packs automatically
Extract a ZIP exported from PixelFx SFX Studio into public/audio/ and iterate manifest.json inside your boot scene so multi-take WAVs register without manual asset lists:
preload() {
this.load.json('sfx-manifest', 'audio/manifest.json');
}
create() {
const manifest = this.cache.json.get('sfx-manifest');
for (const item of manifest.sounds) {
item.files.forEach((relPath, idx) => {
const key = item.files.length > 1 ? item.slug + '_t' + (idx + 1) : item.slug;
this.load.audio(key, 'audio/' + relPath);
});
}
this.load.once('complete', () => this.scene.start('PlayScene'));
this.load.start();
}
4. 16-bit WAV vs compressed loops in HTML5 builds
Short 16-bit mono 44.1 kHz WAV cues (50–250 ms) weigh 4–22 KB uncompressed and loop or re-trigger with zero MP3 encoder padding gaps across desktop and mobile browsers. Only transcode multi-second ambient beds such as rain-loop or wind-loop to OGG/M4A when targeting strict < 2 MB jam bundle limits. Pair this setup with the Phaser & Canvas Pixel VFX Guide.