I have some music playing in my main menu scene and I want to stop that music on game over from within my game scene. How do I do this?
My music code for the main menu is just:
class MainMenu extends Phaser.Scene{
constructor(){
//super() inherits all the characteristics of the Phaser "scene" class
super("menuGame");
}
create(){
//Start playing menu music
this.menuMusic = this.sound.add("menuMusic");
var musicConfig = {
mute: 0,
volume: 0.6,
seek: 0,
loop: false, //DEBUG. Change back to true for final build
delay: 0
}
this.menuMusic.play(musicConfig); // Start Playing the menu bg music
this.scene.start("playGame");
}
}
Now how would I stop menuMusic from within the playGame (Game.js) scene?
Because I want a seamless transition between the menu and game scene. If I started the music again in the game, it wouldnāt be seamless since it would start again from the beginning.
Hmm Iāve never used emitEvent before Iāll definitely check that out, glad to know thereās a proper way to communicate across scenes.
class MainMenu extends Phaser.Scene{
constructor(){
super("menuGame");
}
create(){
let menuMusic = this.sound.add("menuMusic");
var musicConfig = {
mute: 0,
volume: 0.6,
seek: 0,
loop: false, //DEBUG. Change back to true for final build
delay: 0
}
menuMusic.play(musicConfig); // Start Playing the menu bg music
this.scene.start("playGame", {menuMusicData: menuMusic}); // start new scene
}
}
And in my scene B:
update(time, data){
if (gameOver == true){
data.menuMusicData.stop(); //Stop music that was passed to this scene in MainMenu.js
return;
}
Am I doing anything wrong? Can the update() method also access data?
No. update() receive two arguments: time and delta. data is receibed by init() and create().
But you can save data in a property and use it later in update().
Is that in the engine right now? Because I would love to use this since I am recreating the menuMusic object everytime I go back to the main menu anyways.