I am making a game where a lot of the content is user created (eg. drawn by the player), so I am loading a lot of content dynamically, and also in loops, for instance to get arrays of drawings.
So with on ‘loaderror’ handler it would be very difficult to know in which context (function) the file failed to load and how to get the file back in the array it belongs to.
With my solution above I would get in to trouble if there is an other function loading images and getting an error, my resolveLoadError function would only handle one case.
I could check if the loading queue is finished with
this.load.isReady()
or this.load.isLoading()
but that would also not give me absolute certainty which queue is loading
So what I came up now with is making a global this.resolveErrorObjectArray wherein I pass the file that is being loaded, but also the function, index of the array it is part of etc
Then when the callback function for ‘loaderror’ is fired, it looks at that array and filters out the image that got the error.
And then with the information that is object array I can reload the image, and process it further.
But I feel that all this is a bit much to have to do, I have the feeling it could be simpler?
here is my code for reference:
this.resolveErrorObjectArray = []
async getHomeImages(url, element, index, homeImageKey, scene) {
console.log("getHomeImages")
await convertImage(url, "128", "png")
.then((rec) => {
//console.log("rec", rec)
// load all the images to phaser
scene.load.image(homeImageKey, rec)
.on(`filecomplete-image-${homeImageKey}`, (homeImageKey) => {
//delete from this.resolveErrorObjectArray
this.resolveErrorObjectArray = this.resolveErrorObjectArray.filter((obj) => obj.imageKey !== homeImageKey)
console.log("this.resolveErrorObjectArray", this.resolveErrorObjectArray)
//create the home
this.createHome(element, index, homeImageKey, scene)
}, this)
// put the file in the loadErrorCache, incase it doesn't load
this.resolveErrorObjectArray.push({ loadFunction: "getHomeImage", element: element, index: index, imageKey: homeImageKey })
scene.load.start() // start loading the image in memory
})
}
resolveLoadError(offendingFile) {
let resolveErrorObject = this.resolveErrorObjectArray.find(obj => obj.imageKey == offendingFile.key)
let loadFunction = resolveErrorObject.loadFunction
let element = resolveErrorObject.element
let index = resolveErrorObject.index
let imageKey = offendingFile.key
let scene = ManageSession.currentScene
switch (loadFunction) {
case ("getHomeImage"):
console.log("load offendingFile again", imageKey, offendingFile)
scene.load.image(imageKey, './assets/ball_grey.png')
.on(`filecomplete-image-${imageKey}`, (imageKey) => {
//delete from this.resolveErrorObjectArray
this.resolveErrorObjectArray = this.resolveErrorObjectArray.filter((obj) => obj.imageKey !== imageKey)
console.log("this.resolveErrorObjectArray", this.resolveErrorObjectArray)
//create the home
this.createHome(element, index, imageKey, scene);
}, this)
scene.load.start()
break
default:
console.log("please state fom which function the loaderror occured!")
}
}