How to replace a loaded image(cache)?

Hi.

If I loaded an image using
game.load.image(‘object’, ‘assets/sprites/car.png’);
how can I replace that image using the same key?
for example
game.load.image(‘object’, ‘assets/sprites/house.png’);

This is talking about “cache” not about displayed images on the scene.

The problem is that if a load a new image using the same key when I try to add a new image/sprite(game.add.image) using that key I see the first image not the second one.

I load the new image using the load method and adding a event to know when the image was loaded and in the event I add the new image on the scene but if I used the same key I got the old image and if I use a different key then I got the new image.

I dont’ want to have different keys because I only need 1 image at time and I don’t want to preload hundred of images because I just need 1 and because loading hundred of images takes some time before the game starts running.

Or how I can remove from memory/cache the image? I can’t see a method for Phaser 3 only for previos versions.

Thanks.

1 Like
this.textures.remove('object')
this.load.image('object', 'assets/sprites/house.png');
3 Likes

Hi.

That is what I needed. I spend a lot of time searchig for that without luck.

Thanks :smiley:

1 Like

Hi. I have same issue with spritesheet. I load spritesheet for the scene

import enemy from "../assets/sprEnemy.png";
...
preload() {
     this.load.spritesheet('enemy', enemy, {
         frameWidth: 34,
         frameHeight: 42
     });
}
create(){
        ...
        this.enemies = this.add.group();
}
update() {
        for (let i = 0; i < this.enemies.getChildren().length; i++) {
         
        }
}

After end of the stage I want to start this scene again but I want to change spritesheet. I try to make

import enemy2 from "../assets/sprEnemy2.png";
...
preload() {
      this.textures.remove('enemy')
      this.load.spritesheet('enemy', enemy2, {
         frameWidth: 34,
         frameHeight: 42
     });
}

but I have error in update() with this.enemies.getChildren()
Uncaught TypeError: Cannot read property 'entries' of undefined.

How to correct replace spritesheet? I don’t want to preload all sprites.

I don’t think that’s a texture problem. Please make a new topic, thanks.