Sound in Script Done
Fom script user can load, play and stop sound
This commit is contained in:
parent
517c0e86cb
commit
f03f8076f3
16 changed files with 309 additions and 18 deletions
|
@ -1,14 +1,17 @@
|
|||
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
|
||||
import BaseSoundManager = Phaser.Sound.BaseSoundManager;
|
||||
import BaseSound = Phaser.Sound.BaseSound;
|
||||
import Config = Phaser.Core.Config;
|
||||
import SoundConfig = Phaser.Types.Sound.SoundConfig;
|
||||
|
||||
|
||||
class SoundManager {
|
||||
|
||||
private soundPromises : Map<string,Promise<BaseSound>> = new Map<string, Promise<Phaser.Sound.BaseSound>>();
|
||||
public loadSound (loadPlugin: LoaderPlugin, soundManager : BaseSoundManager, soundUrl: string) : Promise<BaseSound> {
|
||||
return new Promise<BaseSound>((res) => {
|
||||
let soundPromise = this.soundPromises.get(soundUrl);
|
||||
if (soundPromise !== undefined) {
|
||||
return soundPromise;
|
||||
}
|
||||
soundPromise = new Promise<BaseSound>((res) => {
|
||||
|
||||
let sound = soundManager.get(soundUrl);
|
||||
if (sound !== null) {
|
||||
return res(sound);
|
||||
|
@ -17,14 +20,20 @@ class SoundManager {
|
|||
loadPlugin.once('filecomplete-audio-' + soundUrl, () => res(soundManager.add(soundUrl)));
|
||||
loadPlugin.start();
|
||||
});
|
||||
this.soundPromises.set(soundUrl,soundPromise);
|
||||
return soundPromise;
|
||||
}
|
||||
|
||||
public async playSound(loadPlugin: LoaderPlugin, soundManager : BaseSoundManager, soundUrl: string, config: SoundConfig) : Promise<void> {
|
||||
console.log("play sound");
|
||||
const sound = await this.loadSound(loadPlugin,soundManager,soundUrl);
|
||||
|
||||
sound.play(config);
|
||||
console.log("j'ai joué le son");
|
||||
|
||||
|
||||
}
|
||||
|
||||
public stopSound(soundManager : BaseSoundManager,soundUrl : string){
|
||||
console.log("stop "+ soundManager.get(soundUrl).key);
|
||||
soundManager.get(soundUrl).stop();
|
||||
}
|
||||
}
|
||||
export const soundManager = new SoundManager();
|
Loading…
Add table
Add a link
Reference in a new issue