How To Delete Images On Screen I No Longer Need?

Hi,

Progressing well.

How can I delete images on the screen I no longer need?
Like when ending a game and returning to the title screen?

Let me know, thanks!

Jesse Lee

You can destroy game objects or (if using multiple scenes) stop the scene they belong to.

Hi,

Thanks for the reply.
I am only using one main scene for the entire game.

Would it be possible to have an array of Phaser 3 images?
Could you provide a small code example to the above idea?

Thanks!

Jesse Lee

function LoadAllVisuals ()
{
    this.load.image('sky', 'assets/sky2.png');
    this.load.image('ground', 'assets/platform2.png');
    this.load.image('star', 'assets/star.png');
    this.load.image('bomb', 'assets/bomb.png');
    this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });

    this.load.image('BG_Screen_Fading', 'data/images/backgrounds/BG_Screen_Fading.png');
    
    this.load.image('GUI_Keyboard', 'data/images/gui/GUI_Keyboard.png');
    this.load.image('GUI_MouseTouch', 'data/images/gui/GUI_MouseTouch.png');

    this.load.image('BG_PHASER_v3_Logo', 'data/images/logos/BG_PHASER_v3_Logo.png');

    this.load.image('BG_JLP', 'data/images/backgrounds/BG_JLP.png');

    this.load.image('BG_Title', 'data/images/backgrounds/BG_Title3.png');

    this.load.image('SPR_Logo', 'data/images/logos/Logo.png');

    this.load.image('SPR_LineOne', 'data/images/gui/Line.png');
    this.load.image('SPR_LineTwo', 'data/images/gui/Line.png');
    this.load.image('SPR_LineThree', 'data/images/gui/Line.png');
    this.load.image('SPR_LineFour', 'data/images/gui/Line.png');
    this.load.image('SPR_LineFive', 'data/images/gui/Line.png');
    this.load.image('SPR_LineSix', 'data/images/gui/Line.png');
    this.load.image('SPR_LineSeven', 'data/images/gui/Line.png');
    this.load.image('SPR_LineEight', 'data/images/gui/Line.png');
    this.load.image('SPR_LineNine', 'data/images/gui/Line.png');
    this.load.image('SPR_LineTen', 'data/images/gui/Line.png');

}
player.destroy();
stars.destroy();

You’ll be destroying game objects, not textures (assets).

If you intend to recreate the game objects later, you will probably want to move to a 2-scene pattern, where all you need to do is restart the scene.

Ok, thanks - I’ll try that!

Sorry:

Is it technically possible to have an array of images?
Image management would be much simpler if I could have an array.
Let me know, thanks!

Jesse Lee

You can make an array of game objects, yes.

Thanks, got it working now:

var SPR_LineDivider = [10];

    for (index = 0; index < 10; index++)
    {
        SPR_LineDivider[index] = this.load.image('SPR_LineDivider[index]', 'data/images/gui/Line.png');
    }

    for (index = 0; index < 10; index++)
    {
        SPR_LineDivider[index] = this.add.image(-99999, -99999, 'SPR_LineDivider[index]');
        SPR_LineDivider[index].setDepth(1);
        DrawSprite(SPR_LineDivider[index], 320, 180, 1.0, 1.0, 0, 255, 255, 255, 0);
    }

DrawSprite(SPR_LineDivider[0], 320, 92, 1.0, 1.0, 0, 255, 255, 255, 255);

Not from load.image(), it doesn’t create or return a game object. And the loop after that is overwriting SPR_LineDivider[0] etc. anyway.

var SPR_LineDivider = [];

for (index = 0; index < 10; index++) {
  SPR_LineDivider[index] = this.add.image(0, 0, "line");
}

Ok, I will fix that, thanks!