Sign in to follow this  
Followers 0
Tufan

How should i delete objects?

5 posts in this topic

Should i set variables to null after removing them? Is it a good practice?

I have 2 states: load and play. I want to delete "Loading..." text when switching state to play but loadingText variable is always Phaser.Text object if i dont set it to null.

My current code is:

var loadingText;

// load state

loadingText = game.add.text(...);

// ...end of load state

game.world.remove(loadingText);

game.state.start("play");

Should i set loadingText variable to null to free memory?

Share this post


Link to post
Share on other sites

Removing the text object would clear up more memory.  I am not sure how much actually its going to save though.

Tufan likes this

Share this post


Link to post
Share on other sites

You don't really have to. When you switch states the world gets cleared and the memory will eventually get released.

Tufan likes this

Share this post


Link to post
Share on other sites
3 hours ago, samme said:

When you switch states the world gets cleared...

But the variable won't be cleared automatically, i think removing object would help a bit.

1Wcdp4e.png

 

Share this post


Link to post
Share on other sites

You can't free memory directly in JS, you can only remove references.

In your case, setting loadingText to null makes no difference because the loadingText reference disappears once the function exits. It doesn't erase the object, which is still in game.world.children somewhere. After Phaser clears the world, there are no more references to the the text object and it eventually passes into oblivion.

Tufan likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.