How to load assets asynchronously with Phaser 3
How to load assets asynchronously with Phaser 3
On Phaser 3 you can pre-load your assets in a function called preload
so they are ready to use when the function create
starts. But what if you need to wait for an API request before loading an asset?
Unfortunately, you can’t simply make the preload
function an async
function and force it to wait for your API calls, but there are ways around it.
Using the Rex Await Loader Plugin
rexrainbow is a great developer that brings a lot to the Phaser community, with a documentation website full of Phaser examples and a bunch of great Phaser plugins.
The Rex Await Loader Plugin can be loaded in the global game context by adding it to the plugins
attribute in the Phaser config.
Then in the preload
scene:
Using the native Phaser Loader
To avoid loading external plugins, there is also an option to use the native Phaser Loader, as described by yannick.
I don’t know how to conclude this post so there you go, I hope this will be useful for someone out there on the internet. If this helped you in any way, don’t forget to leave a comment.
Originally published at https://pablo.gg.