mrvinegar

Loading audio outside preload ()

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

Share this post


Link to post
Share on other sites

I would suggest just adding a preload function to the state you need the audio for. However you can use game.load.audio like you're trying, but if it's not in a preload function you'll have to also start the loader yourself too (and listen for the complete event).

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.