I’m trying to figure out how to work with sounds properly in Phaser 3. My idea is to have 2 sound managers, one for music and another one for sound effects, so I can manage easily the audio of the game.
The thing is that I can’t find a way of doing this. I’ve tried the answer to this old topic: Audio management
But I can’t get it to work because the funcion “create” doesnt exist in Phaser.Sound.SoundManagerCreator.
Afterwards, in my base scene class, I instantiated an small class named “SoundService.ts” like this:
BaseScene.ts:
// Called from standard preload function
protected initServices(): void {
// @ts-ignore: Unreachable code error
this.soundService = new SoundService(this.game.musicAudioManager, this.game.soundAudioManager);
}
Here is the SoundService.ts:
class SoundService {
public music: Phaser.Sound.BaseSoundManager;
public effect: Phaser.Sound.BaseSoundManager;
constructor(musicAudioManager, soundAudioManager) {
this.music = musicAudioManager;
this.effect = soundAudioManager;
}
}
This way I can now use both managers without having to ignore every line of code where I use them. Example of usage from random scene (which inherits from BaseScene.ts):
let button = this.add.image(0, 0, 0);
button.setInteractive();
button.on("pointerdown", function () {
this.soundService.effect.play('click');
}, this);