Jump to content

Load assets after preload


Recommended Posts

Hi all,


I wonder whether it would be possible to load an asset dynamically at a given time in Phaser rather than loading everything in the preload function. The reason for this is simple: I have a game with three different levels, all of which have different background songs; and so I'd rather only load a single song at startup to reduce loading times.


Right now, my preload function looks like this:

preload: function(){    game.load.audio('pixel_world',        ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']);    game.load.audio('second_source',        ['assets/music/second_source_lo.ogg', 'assets/music/second_source_lo.mp3']);    game.load.audio('reboot_complete',        ['assets/music/reboot_complete_lo.ogg', 'assets/music/reboot_complete_lo.mp3']);    game.load.image('pickup', 'assets/img/pickup.png');}

I tried moving one of the game.load.audio() calls to the create function instead:

create: function(){    game.load.audio('pixel_world',    ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']);    // good things follow...}

However, the following calls fail:

this.cache.isSoundDecoded(level.song)// Phaser.Cache.isSoundDecoded: Key "pixel_world" not found in Cache.song = game.add.audio(level.song);// Phaser.Cache.getSound: Key "pixel_world" not found in Cache.

Do you know how I can get this to work, or any other way to ensure that the three songs are not loaded at game startup? Thank you!

Link to comment
Share on other sites

From the documentation, that big unknown for noobs like me:

audio(key, urls, autoDecode) → {Phaser.Loader}

Adds an audio file to the current load queue.

The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.

So basically, game.load.audio() after preload isn't loading the song, just adding it to a queue for later. In order to load the song, I also need to invoke game.load.start():

create: function(){    game.load.audio('pixel_world',        ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']);    game.load.start(); // THIS!    // good things follow...}
Link to comment
Share on other sites

  • 11 months later...

  • Recently Browsing   0 members

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