Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phaser: how to load assets after preload?

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!

like image 290
unpollito Avatar asked Dec 10 '15 22:12

unpollito


People also ask

How do you load assets in phaser?

Let's load the assets we need for our game. You do this by putting calls to the Phaser Loader inside of a Scene function called preload . Phaser will automatically look for this function when it starts and load anything defined within it. This will load in 5 assets: 4 images and a sprite sheet.

How do you add a background image in phaser?

Give this a try: var config = { type: Phaser. AUTO, width: 600, height: 800, physics: { default: 'arcade', arcade: { gravity: {y: 500}, debug: false } }, scene: { preload: preload, create: create, update: update } }; That should load your first scene with the functions you have created.

What is phaser3?

Phaser 3 is the new version of the Phaser Game Framework series. It includes a brand-new custom WebGL renderer designed specifically for the needs of modern 2D games. Phaser uses both a Canvas and WebGL renderer internally and automatically switch between them based on browser support.


1 Answers

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...
}
like image 199
unpollito Avatar answered Oct 03 '22 10:10

unpollito