Javascript Phaser3 Assets Not Loading In Create Function

Viewed 229

I'm trying to do what seems to be basic image loading into my phaser3 game outside Phaser's preload function, but nothing is working.

I tried:

  • Lazy loading function in my utils.js:
dynImgLoader(textureKey,x,y,asset)
{
    let loader = new Phaser.Loader.LoaderPlugin(this.scene);
    loader.image(textureKey,asset);
    loader.once(Phaser.Loader.Events.COMPLETE, ()=>
    {
        console.log('sprite ready for usage');
    })
    loader.start();
}

When called it still gives me the default phaser asset image (not loaded)

  • I tried what this tutorial told me in index.js in create function:
    this.load.image('arc_red', arc_red);
    this.load.onLoadComplete.add(this);
    this.load.start();

Still does not work.

Question:

How do I "dynamically" load assets for a multiplayer web socket game without using the preload function? And why is it that I am doing wrong?

1 Answers

If you use this code in the create function (or other non preload functions) it should work:.

this.load.image(key, url);   // add task
this.load.once('complete', callback, scope);  // add callback of 'complete' event
this.load.start();                     // start loading

I got it from this documentation, and it works in my demo code.

Here a working example on Phaser.io

Related