Hello,
I have a problem with the Phaser images which tells me incorrect sizes.
I have a bunch of images to load. I’m loading them via this.load.image(). After loading them I want to continue with the rest of the code where I’m taking - for instance - width/height and positions. I’m checking the Phaser to see if it downloaded already via:
this.game.textures.gameTextureKeys()
If there is a key to the image I believe it is already loaded. My issue is baseSprite.height and baseSprite.displayHeight, which tells me the incorrect size, 32px. If I set a timeout for 3-4s with console.log to the .height, then it gives me the correct size.
I’m using scene.load.on(“complete”), but it doesn’t do much. It doesn’t matter if the image is cached or not, this is completely random. Sometimes I’m lucky and it works properly.
I can’t test this easily but I would guess the problem is in loadImage().
Remove setTimeout()s, obviously.
Why is this imagesLoaded instead of imageToLoad?
It’s safer and a little more correct to use FILE_KEY_COMPLETE or ADD_KEY. Then you know exactly what was loaded. COMPLETE means every file loaded or failed, or no files were queued after start().
If you do use COMPLETE I think it needs to be like
The promise is an asynchronous function which means that the code will keep running even if the data has not been loaded yet. I think the beheavior is normal. I’m not sure what you are trying to achieve, but the solutions would be eihter to load the image during the preload or to start your game only after the data has been loaded in the callback of the promise.
I decided to create an array with promises, where the promise listens to ‘filecomplete’ event. I’m using Promise.allSettled to ensure everything is done inside an array.
Just to explain — to prevent rendering errors, Phaser gives the “missing” texture (32 × 32) if you try to retrieve a texture that doesn’t exist, either with textures.get(), setTexture(), or creating a game object.