Sign in to follow this  
KevinYen

Memory issue about reload scene & sprite & animation

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 ).

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.