From fc3a503bcf1e4c35847cc89077a96adecb4fb3a9 Mon Sep 17 00:00:00 2001 From: Johannes Berthel Date: Fri, 2 Apr 2021 21:26:24 +0200 Subject: [PATCH] don't fail if companion texture is not found --- front/src/Phaser/Companion/Companion.ts | 10 ++++++---- .../CompanionTexturesLoadingManager.ts | 20 +++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/front/src/Phaser/Companion/Companion.ts b/front/src/Phaser/Companion/Companion.ts index 196da09e..407b7326 100644 --- a/front/src/Phaser/Companion/Companion.ts +++ b/front/src/Phaser/Companion/Companion.ts @@ -36,10 +36,12 @@ export class Companion extends Container { this.companionName = name; - lazyLoadResource(this.scene.load, this.companionName).then(resource => { - this.addResource(resource); - this.invisible = false; - }) + lazyLoadResource(this.scene.load, this.companionName) + .then(resource => { + this.addResource(resource); + this.invisible = false; + }) + .catch(error => console.error(error)); this.scene.physics.world.enableBody(this); diff --git a/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts b/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts index 7023a34d..126af219 100644 --- a/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts +++ b/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts @@ -13,17 +13,17 @@ export const loadAll = (loader: LoaderPlugin): CompanionResourceDescriptionInter } export const lazyLoadResource = (loader: LoaderPlugin, name: string): Promise => { - const resource = COMPANION_RESOURCES.find(item => item.name === name); + return new Promise((resolve, reject) => { + const resource = COMPANION_RESOURCES.find(item => item.name === name); - if (typeof resource === 'undefined') { - throw new TextureError(`Texture '${name}' not found!`); - } - - if (loader.textureManager.exists(resource.name)) { - return Promise.resolve(resource.name); - } - - return new Promise(resolve => { + if (typeof resource === 'undefined') { + return reject(`Texture '${name}' not found!`); + } + + if (loader.textureManager.exists(resource.name)) { + return resolve(resource.name); + } + loader.spritesheet(resource.name, resource.img, { frameWidth: 32, frameHeight: 32, endFrame: 12 }); loader.once(`filecomplete-spritesheet-${resource.name}`, () => resolve(resource.name)); });