class Example extends Phaser.Scene {
preload() {
this.load.audio(
"victory",
"https://assets.codepen.io/9367036/15+8+victory+LOOP.mp3"
);
}
create() {
// Audio
this.victoryMusic = this.sound.add("victory");
this.victoryMusic.setVolume(0);
this.victoryMusic.setLoop(true);
this.victoryMusic.play();
// Bouton audio
const centerX = config.width / 2;
const centerY = config.height / 2;
this.musicButton = this.add
.text(centerX, centerY, "Jouer", {
fontSize: "16px",
fill: "#fff"
})
.setOrigin(0.5)
.setInteractive();
// Volume
this.volumeText = this.add.text(10, 10, `Volume...`, {
fontSize: "12px",
fill: "#fff"
});
// Animation audio
this.isMusicFadingOut = true;
this.musicButton.on("pointerdown", () => {
if (this.isMusicFadingOut) {
this.tweens.add({
targets: this.victoryMusic,
volume: 1,
duration: 1000
});
this.musicButton.setText("Arrêter");
} else {
this.tweens.add({
targets: this.victoryMusic,
volume: 0,
duration: 1000
});
this.musicButton.setText("Jouer");
}
this.isMusicFadingOut = !this.isMusicFadingOut; // Inverser l'état
});
}
update() {
this.volumeText.setText(
`Volume: ${parseInt(this.victoryMusic.volume * 100)}%`
);
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 200,
parent: "phaser-example",
physics: {
default: "arcade",
arcade: { debug: false }
},
scene: Example
};
const game = new Phaser.Game(config);