Jump to content

Waiting until all resources are loaded.


TomCruise
 Share

Recommended Posts

Hey there!

I have two modules - "controller.js" and "loader.js".

In the controller module, I call two methods - `Loader.loadResources()` and right after that, `Sprites.createSprites()`

(which uses the `loader.resources` to create sprites from textures).

As you can probably tell, the latter function throws an error since the loader hasn't completed loading all the resources yet.

I don't want to pass `Sprites.createSprites()` as a callback to the `loader.load()` method since I want the loader module to do only one thing - load resources.

Is there a way to wait until the loader completes loading all resources?

Thanks!

Link to comment
Share on other sites

Hey,

 

I've worked through the similar problem recently and ended up using the Promise API as follows:

    async loadResource(key: string, path: string): Promise<PIXI.LoaderResource> {
        return new Promise<PIXI.LoaderResource>((resolve) => {
            PIXI.Loader.shared.add(key, path).load(() => {
                resolve(PIXI.Loader.shared.resources[key]);
            });
        });
    }



so in your initialization code you can do something like this:

await loaderResource("sprite", "path/to/image.png");
// at this point the resource has been loaded so you can proceed
Sprites.createSprite(...)

Hope that helps

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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