melonJS
    Preparing search index...

    Function preload

    • Declare every bundle of a record and load them all, behind the loading screen: the boot call of a game with several levels, given the record its assets file exports ({ common: [...], level1: [...], ... }).

      Each key becomes a bundle, exactly as loader.setBundle would make it, with the same checks. A bundle already declared with the same assets is left as it is; one declared with other assets is redefined, which throws while it is loaded or loading. Every key is checked before any is declared, so a bad one throws with nothing changed.

      Every bundle is then loaded as one preload, as with a flat list: one progress bar over all their assets (an asset several bundles list is loaded once, and counted once), the shader warm-up, then the callback, LOADER_COMPLETE, and the promise resolves. From there each bundle is loaded as if by load(id): loader.unload(id) frees the assets only that bundle holds, and loader.load(id) brings them back. Calling it again fetches nothing already in memory.

      Parameters

      • bundles: {}

        bundle id → its asset descriptors

      • Optionalonloadcb: Function

        called once every asset has loaded, before LOADER_COMPLETE

      • OptionalswitchToLoadState: boolean

        switch to the loading screen (state.LOADING) while loading

      Returns Promise<void>

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

      synchronously, when bundles is not a record of asset arrays — a single asset descriptor passed by mistake included (wrap it in an array)

      synchronously, when a key names a bundle that is loaded or loading with other assets, or an asset has an unknown type

      // resources.js: what every level uses, then each level's own assets
      export default {
      common: [
      { name: "player", type: "image", src: "img/player.png" },
      { name: "jump", type: "audio", src: "sfx/" },
      ],
      map1: [
      { name: "map1", type: "tmx", src: "map/map1.tmx" },
      { name: "hills", type: "image", src: "img/hills.png" },
      ],
      map2: [
      { name: "map2", type: "tmx", src: "map/map2.tmx" },
      { name: "hills", type: "image", src: "img/hills.png" }, // shared
      ],
      };

      // game.js: the same call as with a flat list
      import resources from "./resources.js";
      await loader.preload(resources);
      state.change(state.PLAY);
    • Load a list of assets behind the loading screen: the boot call.

      It switches to the loading screen (state.LOADING, unless told not to), loads every asset of the list, warms the shaders (see the prewarm application setting), then calls onloadcb, emits LOADER_COMPLETE, and resolves. Each call has only its own callback: a preload given none calls nothing on completion, whatever an earlier preload was given.

      Progress is reported through event.LOADER_PROGRESS as the share of this call's assets loaded so far, reaching 1 with the last of them. A preload started while another is still loading joins it, and the progress then covers the assets of both; once they have all settled, the next preload counts from zero again.

      If an asset fails to load, event.LOADER_ERROR is emitted with it, the promise rejects, and neither the callback nor LOADER_COMPLETE comes; loader.reload retries the asset and then completes this preload.

      The list may also name bundles declared with loader.setBundle, mixed freely with asset descriptors: each id stands for the assets of that bundle, and those bundles are loaded by this call exactly as load(id) would load them. An asset that appears more than once (in two bundles, or in a bundle and directly) is loaded once. An asset listed directly is pinned: no bundle unload frees it, only loader.unload with the descriptor, or loader.unloadAll.

      Parameters

      • assets: (string | Asset)[]

        the asset descriptors to load, and/or ids of bundles declared with loader.setBundle

      • Optionalonloadcb: Function

        called once every asset has loaded, before LOADER_COMPLETE

      • OptionalswitchToLoadState: boolean

        switch to the loading screen (state.LOADING) while loading

      Returns Promise<void>

      resolves once every asset has loaded (after the callback, which is still called: use either form); rejects if one fails to load

      synchronously, on an unknown bundle id (before anything is loaded) or an unknown asset type

      import { loader, state } from "melonjs";

      const resources = [
      // an image, and a texture atlas (its image and JSON descriptor)
      { name: "background", type: "image", src: "data/img/background.png" },
      { name: "texture", type: "image", src: "data/img/texture.png" },
      { name: "texture", type: "json", src: "data/img/texture.json" },
      // a Tiled map, its external tileset and the tileset's image
      { name: "map1", type: "tmx", src: "data/map/map1.tmx" },
      { name: "tileset", type: "tsx", src: "data/map/tileset.tsx" },
      { name: "tileset", type: "image", src: "data/map/tileset.png" },
      // sounds: src is the directory (see audio.init)
      { name: "jump", type: "audio", src: "data/sfx/" },
      // a web font
      { name: "PressStart2P", type: "fontface", src: "data/fnt/PressStart2P.ttf" },
      ];

      // await it...
      await loader.preload(resources);
      state.change(state.PLAY);

      // ...or pass a callback
      loader.preload(resources, () => {
      state.change(state.PLAY);
      });
      // bundles and descriptors in one list
      loader.setBundle("common", commonAssets);
      loader.setBundle("level1", level1Assets);
      await loader.preload([
      "common",
      "level1",
      { name: "logo", type: "image", src: "img/logo.png" },
      ]);