mrvinegar Posted January 28, 2014 Share Posted January 28, 2014 Instead of loading all my audio upfront, i want to do it when each level is loaded, but when I try to load audio outside of preload () it doesn't work, is there a way of doing this? Thanks... Link to comment Share on other sites More sharing options...
Maxime Posted January 28, 2014 Share Posted January 28, 2014 One state per level and use preload method of this state. Using this method each level is responsible for loading its own resources. Link to comment Share on other sites More sharing options...
mrvinegar Posted January 28, 2014 Author Share Posted January 28, 2014 One state per level and use preload method of this state. Using this method each level is responsible for loading its own resources. Would you have an example of this? Link to comment Share on other sites More sharing options...
jerome Posted January 28, 2014 Share Posted January 28, 2014 you can load files where you wantit 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 More sharing options...
mrvinegar Posted January 28, 2014 Author Share Posted January 28, 2014 Thanks, but when I try "game.load.audio" outside preload(), i.e after create is called when a button is clicked, the referenced audio doesn't load, I don't see an error, but the audio doesn't load... Link to comment Share on other sites More sharing options...
rich Posted January 28, 2014 Share Posted January 28, 2014 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). Link to comment Share on other sites More sharing options...
johncip Posted September 18, 2015 Share Posted September 18, 2015 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.) Tom Atom 1 Link to comment Share on other sites More sharing options...
sagar Posted January 31, 2019 Share Posted January 31, 2019 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 More sharing options...
Recommended Posts