Assets Manager. Tilemaps(.tmj, .json, .tmx, .xml), images and audio files loading and managing.
- Install module
npm i assetsm
- Import and create a class instance
import AssetsManager from "assetsm"
const assets = new AssetsManager()
- Register files
assets.addAudio(key, url)
assets.addImage(key, url)
assets.addTileMap(key, url)
- Subscribe for progress to track the loading progress status
assets.addEventListener("progress", (event) => {
console.log("progress, loaded items: ", event.loaded);
console.log("progress, items left: ", event.total);
});
- Get current pending uploads if necessary
assets.filesWaitingForUpload
- Preload all files you registered in the previous step
assets.preload().then(() => {
- Use files
{
const audio = assets.getAudio(key)
const image = assets.getImage(key)
const tilemap = assets.getTileMap(key)
}
- To check the process you can subscribe for ProgressEvent.type event
// fires when uploading is starting
assets.addEventListener("loadstart", () =>
// fires when uploading is in progress
assets.addEventListener("progress", () =>
// fires when uploading is over
assets.addEventListener("load", () =>
// load errors
assets.addEventListener("error", (err) =>
npm i --save-dev
npm start
- Images are loaded as ImageBitmaps
- When loading tilemaps, the process also handles tileset files and loads the images contained within them.
- ES6 only
- Register a loader and uploadMethod using registerLoader(loaderType, loaderMethod)
- Add upload item to the queue using add[loaderName](fileKey, url), or addFile(loaderName, fileKey, url).
- Executing preload(), will upload all items where added in step2 with loaderMethod provided in step1 and save them temporary.
- After that uploadingResults will be available with getloaderName, or getFile(loaderName, fileKey, url)
If you want to load tilesets separately, pass false as 3d parameter to addTileMap and then use addTileSet to add tileset to queue:
assets.addTileMap(key, url, false);
assets.addTileSet(key, url);
assets.preload().then(() => {
assets.getTileMap(key);
assets.getTileSet(key);
...
Two new loaders added: AtlasXML, AtlasImageMap. After uploading atlas xml, atlasImageMap will be loaded, and individual images could be accessed with getImage():
assets.addAtlasXML(key, url);
assets.preload().then(() => {
const atlasImageMap = assets.getAtlasImageMap(key),
someImageFromAtlas = assets.getImage(imageKey),
;
...
- Critical issues. The behavior: stop upload and reject the promise.
- addFileType() method, file key or url is incorrect
- incorrect file extension
- incorrect uploadMethod return type
- upload recursion error
- Non critical issues. The behavior: continue upload process, failed object will be assigned to the null value, warning will be shown in the console.
- all other issues, such as 404
- Non critical issues.
- #1: Empty AssetsManager preload() never resolves
- .tmx/.xml tilemap and .tsx/.xml tileset support
- inline tileset support
- removed gid
- loaderMethod should return Promise with uploading result value
- loaderMethod is optional, by default it will return fetch result