Husky Posted February 13, 2014 Share Posted February 13, 2014 Hi All, I'm having some problems with memory in my game and I believe it's related with how I create and destroy texts in my game. To test it I created a small test app and I'm using the Chrome Developer Tools.GameVar = {} GameVar.GameScreen = function (game) {}GameVar.GameScreen.prototype = { create: function() { this.add.existing(new Phaser.Text(this.game, Math.floor((Math.random()*600)+1), Math.floor((Math.random()*500)+1), "Test", {font: "14pt TepenoSans", fill: "#000000"})); }, shutdown: function() { }, update: function() { if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { this.game.state.start('GameScreen'); } if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) { this.add.existing(new Phaser.Text(this.game, Math.floor((Math.random()*600)+1), Math.floor((Math.random()*500)+1), "Test", {font: "14pt TepenoSans", fill: "#000000"})); } },};This is how I'm doing the test: 1 - Enter the app2 - Start recording on Timeline > Memory3 - Press "Enter" for a few seconds4 - Press "Space" to restart my "state"5 - Press the Garbage Collector 6 - Stop the recording Using the dev tool I can see that some of my texts are still in memory. Do I have to do something on "shutdown" function to free this texts? Thanks in advance,Felipe. Link to comment Share on other sites More sharing options...
rich Posted February 13, 2014 Share Posted February 13, 2014 Could you re-use the Text objects? Rather than create new ones? As every time you make one it makes another new canvas object to render it from. Link to comment Share on other sites More sharing options...
Husky Posted February 14, 2014 Author Share Posted February 14, 2014 Hi rich, Thanks for answering . In this case yes, I could re-use my texts objects. But on my app I have several states with lots of Texts (Help, Menu, Stats and in game HUD and texts) with different sizes and fonts. Should I re-use this objects too? Is there a way to force a "free" on Text objects? Thanks again,Felipe. Link to comment Share on other sites More sharing options...
Husky Posted February 14, 2014 Author Share Posted February 14, 2014 A few more tests When adding inside a group after a change state all the texts are still on memory//ADDING INSIDE A GROUPthis.screenElements = this.add.group();for (var i = 0; i < 5; i++) { this.screenElements.add(new Phaser.Text(this.game, 500, 30 * i, "screenElements.add " + i)); };When adding directly only the first text remains on memory after the change state//ADDING DIRECTLYfor (var i = 0; i < 5; i++) { this.add.text(0, 30 * i, "add.text " + i);};And when I'm creating the texts and don't add in any group all the texts are removed from memory after a change state//JUST CREATING BUT NO ADDINGthis.text0 = null;this.text1 = null;this.text2 = null;this.text3 = null;this.text4 = null;for (var i = 0; i < 5; i++) { this["text"+i] = new Phaser.Text(this.game,500, 30 * i, "screenElements.add (var) " + i);};Thanks,Felipe. Link to comment Share on other sites More sharing options...
Recommended Posts