Jump to content

Good tips for memory management in Phaser ?


woratana
 Share

Recommended Posts

Hello,

 

I finish making a simple game today with Phaser:

http://spacerobot-html5.bitballoon.com/

 

I found that in some desktop or mobile device, the game lag a little bit after I press 'PLAY AGAIN' button. I think this is because I have to  clear out some variables but I'm not sure how to do this in Phaser.

 

So my question is: Does anyone has good tips for memory management / clear out variables data in Phaser?

Most elements I used in my game is group, sprite, text, and sound.

Link to comment
Share on other sites

I didn't check on the code, but as far as I've seen, you should make sure you are reusing sprites inside a group.

 

When you want to spawn a new enemy, you look for a enemy sprite whose exists value is "false" and then make it alive again in the position that you  want, instead of spawning new ones.

 

If you are doing it that way I would need to check the code before I can think of another problem.

Link to comment
Share on other sites

Hello, cool game!

 

I usually explicitly dispose of all of the objects I create in the shutdown function of each state (assuming you're using states for your game). It looks like this:

shutdown: function () {        //destroy bitmap text        this.creditText = null;        //destroy tween        if (this.creditTween) {            this.creditTween.onComplete.removeAll();            this.creditTween.stop();            this.creditTween = null;        }        //destroy sound        if (this.clickSnd) {            this.clickSnd.stop();            this.clickSnd = null;        }                        //destroy button        if (this.soundButton) {            this.soundButton.kill();            this.soundButton = null;        }                        //destroy sprite        if (this.backing) {            this.backing.destroy();            this.backing = null;        }        console.log('destroy mainmenu');    }
Link to comment
Share on other sites

@Mariogarranz:

Thank you for your suggestion! :) I didn't do that in the game. I would like to ask what if I don't know the maximum number of that group (e.g. It is bullet group and I don't know how much bullet will be spawn at the same time on the screen). What is your solution in this case?

@Hsaga

I'm glad you like the game! :)

I don't know how to do the state. Is there any reference example I can find?

Link to comment
Share on other sites

Actually, could I add a question here please?:

 

Should other variables and arrays, not just phaser objects,  also be set to null on state change?  Or are these cleaned up as part of the state change in phaser?

 

For example score variables or arrays of strings. 

Link to comment
Share on other sites

When dealing with the states, remember that the constructor is called once, and the create function is called every time you switch to the state.

So if you are not resetting all the variables in the create function, you will have left over data from the last time the state was run.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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