melonJS
    Preparing search index...

    Function load

    • Declare a bundle and load it, in one call: exactly loader.setBundle with these assets followed by load(id), so the same checks, the same progress and shader warm-up, LOADER_COMPLETE, and the same promise. Handy where a level's assets are declared next to the code that enters it.

      Called again with the same assets (same type, name and src, in any order), it simply loads the bundle, fetching nothing already in memory. With different assets it redefines the bundle, which is allowed only while that bundle is not loaded or loading, and throws synchronously otherwise, as setBundle does. Release it with loader.unload and its id.

      Parameters

      • id: string

        the bundle id

      • assets: Asset | Asset[]

        the bundle's asset descriptors (one descriptor on its own is accepted too)

      Returns Promise<void>

      resolves once every asset of the bundle has loaded; rejects if one fails to load

      synchronously, on an invalid id or asset list

      synchronously, when the bundle is loaded or loading with other assets, or an asset has an unknown type

      // a level's assets, declared where they are loaded
      await loader.load("level2", [
      { name: "level2", type: "tmx", src: "data/map/level2.tmx" },
      { name: "tiles", type: "image", src: "data/img/tiles.png" },
      ]);
      await level.load("level2", { async: true });
      // ...and later, by id as usual
      loader.unload("level2");
    • Load a bundle declared with loader.setBundle (or by a record passed to loader.preload), while the game keeps running.

      Every asset of the bundle that is not already in memory is loaded, then the shaders are warmed, as loader.preload does: LOADER_PROGRESS reports this call's assets (each asset is one step, and one already in memory counts as a step done at once), then onload is called, LOADER_COMPLETE is emitted and the promise resolves. Unlike preload it does NOT switch to the loading screen; to show it for a level, use loader.preload([id]).

      The bundle then holds its assets: they stay in memory until loader.unload with the same id releases them, and are freed then unless another loaded bundle lists them too, or they were loaded the old way (by preload / load with a descriptor), which pins them. Assets keep their own names: getImage("tiles") reads a bundle's image as any other.

      If an asset fails to load, LOADER_ERROR is emitted with it, onerror is called and the promise rejects; the assets that did load stay, held by the bundle, and loader.reload retries the one that failed.

      Parameters

      • id: string

        the id of a declared bundle

      • Optionalonload: Function

        called once every asset of the bundle has loaded, before LOADER_COMPLETE

      • Optionalonerror: Function

        called with the error if an asset fails to load (the returned promise then needs no catch of its own)

      Returns Promise<void>

      resolves once the bundle is loaded, with or without the callbacks; rejects if an asset fails to load

      synchronously, when no bundle is declared under that id

      // before entering a level that may have been unloaded: fetches only what
      // is missing, and nothing at all when the bundle is still in memory
      await loader.load("level1");
      await level.load("level1", { async: true });
      // the next level's assets, loaded while the current one keeps playing
      loader.load("level3", () => {
      nextLevelReady = true;
      });
    • Load a single asset, and wait for it: for an asset needed during the game that was not preloaded.

      No loading screen, progress or loader event: the promise is the only signal. An asset already in memory resolves at once, without a request (for every type but "binary", "fontface", "aseprite" and "js", which are fetched again). The asset is pinned: unloading a bundle that also lists it does not free it, loader.unload with the descriptor does.

      Parameters

      • asset: Asset

        the asset to load

      Returns Promise<void>

      resolves once the asset has loaded; rejects with the error if it fails to load

      synchronously, when the asset type is unknown

      await loader.load({ name: "avatar", type: "image", src: "data/img/avatar.png" });
      const avatar = new Sprite(0, 0, { image: "avatar" });
    • Load a single asset, with callbacks: the original form of load(asset), for an asset needed during the game that was not preloaded.

      No loading screen, progress or loader event: the callbacks are the only signal. The asset is pinned: unloading a bundle that also lists it does not free it, loader.unload with the descriptor does.

      Parameters

      • asset: Asset

        the asset to load

      • Optionalonload: Function

        called once the asset has loaded. Not called for an asset already in memory: the function then returns 0 (for every type but "binary", "fontface", "aseprite" and "js", which are fetched again)

      • Optionalonerror: Function

        called with the error if it fails to load

      Returns number | Promise<void>

      the number of resources it started loading (usually 1), or 0 when the asset was already in memory. With onload and onerror both undefined it is the promise form above, and returns a promise

      synchronously, when the asset type is unknown

      loader.load(
      { name: "bgmusic", type: "audio", src: "data/bgm/" },
      () => audio.play("bgmusic"),
      (error) => console.warn("no music", error),
      );