Jump to content

How to stop music from Boot.js state?


Mizukage
 Share

Recommended Posts

Hello again,

 

Today I am going crazy with menu background music.

 

I had Boot -> Preloader -> MainMenu -> Game  states and in MainMenu state was 

		this.music = this.add.audio('MenuMusic', 0.2, true);		this.music.play();

And  

this.music.stop();

In start game state  function. It was working fine, music in menu and music off when game started. But I added new state call About.js

And after over 9000 trials I find some way to do sound in Menu and About without stop music and play again from begin.

 

I drop this.add.audio and this.music.play() in to create function from Boot.js State, then go to Preloader and then to MainMenu 

there I do nothing with this music, when I go from MainMenu state to About state music still play and when I will back to MainMenu music is still plaing and its perfect but then cant use

this.music.stop() 

When want go to Game state, because sound was started in Boot state..... 

 

The question is how can I stop this music in Game state??

Link to comment
Share on other sites

Hello,

 

I would wait for others to answer so just my two cents.

 

Set your music to global variable, then you can access it from any other state.

If your game is set as var game = new Phaser.Game(..);, then you can instead of this.music = this.add.audio(..); use this: game.music = this.add.audio(..); or any other variable you create for this from global scope.

 

Well I think there is a way to send some parameters from state to state in phaser like this or this but I think global is the easiest way here.

Link to comment
Share on other sites

Is your music playing or not?

 

Btw yesterday you wrote something about problems within loader or something like that, so just to clarify there is no connection between your thought problem and the solution I offered - that problem doesn't exist.

 

In your preloader load your music as you do:

this.load.audio('MenuMusic', pathToTheFile);

There is no problem with that, you don't need global var for this.

 

Then when you get to your menu (or wherever you want to play the music) place this line:

game.menuMusic = this.add.audio('MenuMusic');game.menuMusic.play();

Then last part when you are leaving the menu and what to stop the music just insert this line into your code (doesn't matter which state you use this line as long as it is after the line above and after your audio file loaded!):

game.menuMusic.stop();

That's what I was talking about, jsut to make sure. I have no idea what exactly are you doing with that muzyka: 1, because you provided no other code (we have no idea what line 16 in your code is or what is before it) ;-).

Link to comment
Share on other sites

But it's working with this Global only ;)

I Need music without breakes when Im jumping by states Menu and About and stop this music only when I go to Game state. 

 

Boot.js

var ShinobiBattle = {  muzyka: null};ShinobiBattle.Boot = function (game) {};ShinobiBattle.Boot.prototype = {    init: function () {        //  Unless you specifically know your game needs to support multi-touch I would recommend setting this to 1        this.input.maxPointers = 1;        //  Phaser will automatically pause if the browser tab the game is in loses focus. You can disable that here:        this.stage.disableVisibilityChange = true;        if (this.game.device.desktop)        {            //  If you have any desktop specific settings, they can go in here            this.scale.pageAlignHorizontally = true;        }        else        {            //  Same goes for mobile settings.            //  In this case we're saying "scale the game, no lower than 480x260 and no higher than 1024x768"            this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;            this.scale.setMinMax(480, 260, 1024, 768);            this.scale.forceLandscape = true;            this.scale.pageAlignHorizontally = true;        }    },    preload: function () {        //  Here we load the assets required for our preloader (in this case a background and a loading bar)        this.load.image('preloaderBG', 'assets/imgTest/loading_screen.png');            },    create: function () {      		ShinobiBattle.muzyka = this.add.audio('MenuMusic', 0.2, true);		ShinobiBattle.muzyka.play();        //  By this point the preloader assets have loaded to the cache, we've set the game settings        //  So now let's start the real preloader going        this.state.start('Preloader');    }};

Then nothing special in preloader

 

MainMenu

ShinobiBattle.MainMenu = function (game) {    this.music = null;    this.playButton = null;    this.aboutButton = null;};ShinobiBattle.MainMenu.prototype = {	create: function () {		this.add.sprite(0, 0, 'MenuBG');		this.playButton = this.add.button(10, 430, 'playButton', this.startGame, this, 'buttonOver', 'buttonOut', 'buttonOver');        this.aboutButton = this.add.button(10, 230, 'playButton', this.startAbout, this, 'buttonOver', 'buttonOut', 'buttonOver');	},	update: function () {	},	startGame: function (pointer) {		      ShinobiBattle.muzyka.stop();		this.state.start('Game');	},      startAbout: function (pointer) {                this.state.start('About');    }};

About

ShinobiBattle.About = function (game) {	this.backButton = null;    };ShinobiBattle.About.prototype = {	create: function () {		this.backButton = this.add.button(10, 430, 'playButton', this.MenuGame, this, 'buttonOver', 'buttonOut', 'buttonOver');	},	update: function () {	},	MenuGame: function (pointer) {		this.state.start('MainMenu');	}};

So music start in Boot and I'm cutting music only from MainMenu to Game

Link to comment
Share on other sites

Well if it's solved then tehre is no need to extend this thread any further.

 

I don't know what exactly was your last psot about but it's exactly the same thing as I wrote above just with different name that's all, so glad you managed to get over this - the error disappeared I guess.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...