Can I load things out of the preload function?

Hi!

I created a custom arrow function inside my class to load things afterwards, but the event filecomplete is never being triggered (not loading anything), so I would like to know if I actually CAN do this. My code:

LoadLevelMap = (level) => {
        this.load.json('level'+level, 'data/level'+level+'.json');
        this.load.on('filecomplete-json-level'+level, (key, type, data) => {
            console.log('json loaded')
            this.load.image('scnMap'+level,'assets/images/Map'+level+'.png');
            this.load.on('filecomplete-image-scnMap'+level, (key, type, data) => {
                console.log("map loaded")
                this['scnPlay'+level] = this.add.image(0,0), "scnMap"+level);
            });
        });
    }

Any idea?

Thanks!

Where/when are you calling LoadLevelMap(…)?

Samme, it’s called by another function which runs when the user reaches the end of the level (to load the next level). Pretty much like this:

EndLevel = () => {
this.imgCongrats.setVisible(true);
this.Level++;
this.waiting.timedPassed = this.time.addEvent({
    delay: 4000,
    callback: this.LoadLevelMap,
    args:[Level],
    callbackScope: this,
    repeat: 0
});
}

EDIT: Both functions are at the same level of preload, create and update functions.

You need to call this.load.start() each time.

1 Like

Cool, it did the trick! Thanks, Samme! :grinning: