Jump to content

Memory issue about reload scene & sprite & animation


KevinYen
 Share

Recommended Posts

I want to restart game scene, and I hope that it doesn't occur memory issue.

Code_1:

create() {
	for (var i = 0; i < 1000; i++) {
        group.get(Math.random() * 640, Math.random() * 360).anims.play(animKey);
    }
	this.input.keyboard.on("keydown_A", (event) => {
        this.scene.start(sceneKey);
	});
}

When I press keydown_A 10 times, look following snapshot, memory will never release

image.png.6c3558475eed1df01559dd4ed05eedb7.png

Why occur this?

Because AnimationManager never run sprite.anims.remove function.

So sprite's reference will keep in game.anims._events.

There are 2 ways to release memory:

First:

We need to implement shutdown function to game scene, and remove AnimationManager's events.

create() {
	this.events.once("shutdown", this.shutdown, this);
	for (var i = 0; i < 4000; i++) {
        group.get(Math.random() * 640, Math.random() * 360).anims.play(animKey);
    }
	this.input.keyboard.on("keydown_A", (event) => {
        this.scene.start(sceneKey);
	});
}
shutdown() {
    game.anims.removeAllListeners();
}

Second:

To implement onDestroy function on every sprite.

create() {
	for (var i = 0; i < 4000; i++) {
        var sp = group.get(Math.random() * 640, Math.random() * 360).anims.play(animKey);
		sp.once("destroy", function () {
            sp.anims.destroy();
        });
    }
	this.input.keyboard.on("keydown_A", (event) => {
        this.scene.start(sceneKey);
	});
}

 

Finally, I want to ask one question.

Why Phaser3 game engine call sprite.destroy and sprite.anims.destroy in passing? ( phaser-3.10.1\src\gameobjects\sprite\Sprite.js, phaser-3.10.1\src\gameobjects\GameObject.js )

I need to implement it by myself.

But Phaser2 will release all of sprite used memory( When I restart Level ).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...