Jump to content

Loading audio outside preload ()


mrvinegar
 Share

Recommended Posts

you can load files where you want

it just won't be good idea to do it in a game loop (update or render)

 

but you can add a preload property in each of your states

 

tuto here : http://s-nambiar.com/tutorials/managing-game-states-phaser/

doc here : https://github.com/photonstorm/phaser/wiki/Phaser-General-Documentation-:-States

Link to comment
Share on other sites

  • 1 year later...

I'm a little late to the party, but for anyone else looking for how to do what Rich is describing, here's how I accomplished it:

playMusic: function () {  this.asyncLoad('song', 'song.mp3', function () {    var music = game.add.audio('song');    music.play();  });},asyncLoad: function(cacheKey, path, onLoad) {  this.load.audio(cacheKey, path);  this.load.start(); // start the loader  var index = window.setInterval(function() {    if (game.load.isLoading) {      return;    }    onLoad();    window.clearInterval(index);  }, 1000);}

Music is the last & largest item loaded, so it suffices to check isLoading (or hasLoaded) on some interval.

this here refers to the current state. game is a global but could also have been this.game (though that would probably require binding the callbacks).

(My other assets are small, and I don't need the music to start immediately.)

Link to comment
Share on other sites

  • 3 years later...
On 9/19/2015 at 4:25 AM, johncip said:

I'm a little late to the party, but for anyone else looking for how to do what Rich is describing, here's how I accomplished it:

playMusic: function () {  this.asyncLoad('song', 'song.mp3', function () {    var music = game.add.audio('song');    music.play();  });},asyncLoad: function(cacheKey, path, onLoad) {  this.load.audio(cacheKey, path);  this.load.start(); // start the loader  var index = window.setInterval(function() {    if (game.load.isLoading) {      return;    }    onLoad();    window.clearInterval(index);  }, 1000);}

Music is the last & largest item loaded, so it suffices to check isLoading (or hasLoaded) on some interval.

this here refers to the current state. game is a global but could also have been this.game (though that would probably require binding the callbacks).

(My other assets are small, and I don't need the music to start immediately.)

@johncip and @rich

It works, but it removes all over progress on a game which we get before the loading

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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