melonJS
    Preparing search index...

    Namespace loader

    The asset loader: it fetches the files a game needs, parses each one according to its type, and keeps the result in memory under the asset's name. Engine classes then take that name directly (new Sprite(0, 0, { image: "player" }), level.load("map1"), audio.play("jump")), and the getters return the parsed asset itself (loader.getImage("player"), loader.getJSON("atlas")...).

    Declaring assets. Every asset is an Asset descriptor, { name, type, src }, where the type picks the parser. The loader takes them in one of two shapes:

    • a flat array of descriptors: the simplest form, for a single-screen game or a prototype. Everything is loaded once, at boot, and stays.
    • a record of bundles, { common: [...], level1: [...], ... }, for a game with several levels or screens. Each key becomes a bundle: a named list of the same descriptors, with a lifetime of its own (see below).

    Asset types. The type of a descriptor picks its parser; Asset lists every one, with the files it takes and how the asset is read back. In short: "image", "aseprite", "json", "binary", "js", "audio", "video", "fontface" and "shader" for what a game draws, plays and reads; "tmx", "tsx", "tilemap", "spritefusion" and "ldtk" for levels made in a map editor; "obj", "mtl", "gltf" and "glb" for 3D.

    Levels. A map is an asset like any other, and loading one also registers it with the level director: level.load then takes its name. That goes for a Tiled map ("tmx"), a tile map from any other tool ("tilemap"), a Sprite Fusion map ("spritefusion"), each level and each world of an LDtk project ("ldtk") and a glTF scene ("gltf" / "glb"). A Tiled map's images are assets of their own, to list beside it; a Sprite Fusion map and an LDtk project bring theirs with them, and free them when they are unloaded.

    preload or load. preload is the boot call: it switches to the loading screen, loads everything it is given, warms the shaders, then calls its callback, emits LOADER_COMPLETE and resolves. load loads a single asset, or a bundle by its id, while the game keeps running.

    Bundles. A bundle is a lifetime, not a namespace: its assets keep their own names and live in the same caches as any other, so getImage("hills") and level.load("map1") read them as usual. Declare one with setBundle (or as a key of a record passed to preload), load it with load or preload, test it with isLoaded, and release it with unload.

    • Bundle ids are strings and assets are objects: loader.unload("map1") releases the bundle "map1", while loader.unload({ name: "map1", type: "tmx" }) frees that one asset.
    • An asset listed by several bundles is fetched once, and freed only when the last loaded bundle listing it is unloaded: list a shared asset in every bundle that uses it.
    • An asset loaded the old way, from a flat array or by load(asset), is pinned: no bundle unload frees it, only unload(asset) or unloadAll().
    • Freeing a map (or a glTF scene) also unregisters its level, so level.load of it throws until its bundle is loaded again.
    • A shader handed out by getShader is freed with the last bundle that lists it, whatever still draws with it: an effect that outlives a level (a HUD's, a camera's) belongs in a bundle that outlives it too.

    Freeing an asset releases what the engine made from it as well: the GPU texture uploaded from an image, the program compiled from a shader, a font's place in the document.

    Settings. setBaseURL prefixes the src of every asset of a type, setOptions sets the CORS and cache-busting options, and setParser teaches the loader an asset type of its own.

    Events. preload and load(id) report through the event bus: event.LOADER_PROGRESS (the share of the call's assets loaded, 0 to 1, and the asset), event.LOADER_ERROR (an asset that failed, which reload retries) and event.LOADER_COMPLETE.

    import { loader } from "melonjs";

    // a single-screen game: one flat list, loaded behind the loading screen
    await loader.preload([
    { name: "player", type: "image", src: "data/img/player.png" },
    { name: "jump", type: "audio", src: "data/sfx/" },
    ]);
    state.change(state.PLAY);
    // levels from a map editor, each loaded as the editor saves it
    await loader.preload([
    // Tiled: the map, and the image its tileset names, under the file's name
    { name: "cave", type: "tmx", src: "data/map/cave.tmx" },
    { name: "tiles", type: "image", src: "data/img/tiles.png" },
    // Sprite Fusion: the map alone, spritesheet.png is next to it
    { name: "tower", type: "spritefusion", src: "data/tower/map.json" },
    // LDtk: the project alone, with every level it holds
    { name: "world", type: "ldtk", src: "data/world/world.ldtk" },
    ]);
    level.load("cave");
    level.load("tower");
    level.load("world:Entrance"); // one level of the project, by its identifier
    // a game with several levels: an assets file exporting one bundle for
    // what every level uses, and one per level

    // resources.js
    export default {
    common: [
    { name: "player", type: "image", src: "data/img/player.png" },
    { name: "tileset", type: "image", src: "data/img/tileset.png" },
    { name: "tileset", type: "tsx", src: "data/map/tileset.tsx" },
    { name: "jump", type: "audio", src: "data/sfx/" },
    ],
    level1: [
    { name: "level1", type: "tmx", src: "data/map/level1.tmx" },
    { name: "hills", type: "image", src: "data/img/hills.png" },
    ],
    level2: [
    { name: "level2", type: "tmx", src: "data/map/level2.tmx" },
    { name: "hills", type: "image", src: "data/img/hills.png" }, // shared
    ],
    };

    // game.js: boot, everything behind the loading screen
    import resources from "./resources.js";
    await loader.preload(resources);

    // later: level 1 will not be played again, so free what only it uses
    // ("hills" stays, level2 lists it too)
    await level.load("level2", { async: true });
    loader.unload("level1");

    // ...and should the player go back to it after all
    await loader.load("level1");
    await level.load("level1", { async: true });
    getBinary
    getBundle
    getFont
    getGLTF
    getImage
    getJSON
    getLDtk
    getMTL
    getOBJ
    getShader
    getTileMap
    getTMX
    getVideo
    isLoaded
    load
    preload
    reload
    setBaseURL
    setBundle
    setOptions
    setParser
    unload
    unloadAll
    Asset
    GLTFData
    GLTFNode
    baseURL
    crossOrigin
    withCredentials