vcarluer Posted September 11, 2014 Share Posted September 11, 2014 I am trying to find what to destroy in shutdown method of states to keep browser memory clean.I have read some posts on this question but I'm not sure to fully understand.By the same time I try to fully understand the GameObjectFactory. Can someone confirm me than calling in a state this.add is the same than calling this.game.add? this.add is only an alias to this.game.add? Or not... In a state, when I add a new object like a sprite and I affect it to this.game, this object will be available in all states?this.game.ground = this.game.add.tileSprite(0, 756, 640, 213, 'ground');Or Phaser destroys all added game objects between states? If I can use this.game.ground in all states, I certainly have to manually destroy it when needed by calling:this.game.ground.destroy();this.game.ground = null;If instead I affect this sprite to the current state what do I have to do?this.ground = this.game.add.tileSprite(0, 756, 640, 213, 'ground')* Destroy it in the state's shutdown function?* Nothing: Phaser will destroy it? Phaser destroys all objects added to game with this.game.add? If it's not clear I plan to add all new objects to local state object and clean them all in shutdown function to be sure but it is a painful work. Link to comment Share on other sites More sharing options...
JUL Posted September 11, 2014 Share Posted September 11, 2014 "Nothing: Phaser will destroy it? Phaser destroys all objects added to game with this.game.add?" Phaser is supposed to destroy everything in the state if you switch state, yes.As long as all object are created within the sate as I understand it. Now depends on how you coded the whole thing, if your approach was rather orthodox or totally heterodox.The later might produce unexpected results. Link to comment Share on other sites More sharing options...
eguneys Posted September 11, 2014 Share Posted September 11, 2014 I've explained the GameObjectFactory and GameObjectCreator here. Phaser has a default root group named game.world, that is destroyed in each state transition and all the children will be destroyed recursively as well. So you don't need to destroy anything manually. JUL 1 Link to comment Share on other sites More sharing options...
rich Posted September 11, 2014 Share Posted September 11, 2014 this.game.ground = this.game.add.tileSprite(0, 756, 640, 213, 'ground');Please don't do this! Adding new properties onto Phaser.Game is a really bad idea. If you need a global object then use something similar to the way the Basic game template does it for sound fx. JUL 1 Link to comment Share on other sites More sharing options...
vcarluer Posted September 11, 2014 Author Share Posted September 11, 2014 Thanks for your answers.And the state object is destroyed too? So the reference to objects are garbage collected?This is for this case:this.ground = this.game.add.tileSprite(0, 756, 640, 213, 'ground')the state holds a reference to the tileSprite with this.ground. Link to comment Share on other sites More sharing options...
vcarluer Posted September 11, 2014 Author Share Posted September 11, 2014 After reading some phaser source code I think states must set their references to game objects to null in shutdown function.I don't see how this objects can be garbage collected otherwise. Phaser Game object holds a reference on StateManager object which references all state objects in its states array.If one state object references a new sprite object for instance with the code above (which is a code I have seen in many examples), the following line must be set in shutdown I think:this.ground = null;Or maybe I've missed something? Next time the state will be set active by State Manager, the state will replace its references (this.ground) to a newly created object.So old references will then be collected by garbage collector.But in the mean time the objects cannot be released.It can be a problem too for a state which can never be reached again for example.And it's a bigger problem for quite huge object created like this. So their won't be any memory leak if object references are not set to null but too much memory is used which can sometimes be an issue.A good practice would be to set to null 'big' objects referenced by states in shutdown. Does someone which knows the framework better than me can confirm this? Link to comment Share on other sites More sharing options...
rich Posted September 11, 2014 Share Posted September 11, 2014 State.shutdown isn't meant to destroy the State, it's just meant to temporarily deactivate it, ready for moving on to another one. It's incredibly rare to need a State you absolutely never return to again, but if you do then yes it's sensible to null out any objects it created. It will still have references to Phaser.Game and various other places, but it's just a reference, they're not expensive. If you need to nuke a state entirely then something like this would clear all Phaser created references:this.game = null;this.add = null;this.make = null;this.camera = null;this.cache = null;this.input = null;this.load = null;this.math = null;this.sound = null;this.scale = null;this.state = null;this.stage = null;this.time = null;this.tweens = null;this.world = null;this.particles = null;this.rnd = null;this.physics = null; JUL and shohan4556 2 Link to comment Share on other sites More sharing options...
J_Wotoo Posted September 16, 2014 Share Posted September 16, 2014 this.game.ground = this.game.add.tileSprite(0, 756, 640, 213, 'ground');Please don't do this! Adding new properties onto Phaser.Game is a really bad idea. If you need a global object then use something similar to the way the Basic game template does it for sound fx. So something like this instead?var MyFirstGame = MyFirstGame || { // global variables player: null, level: 0, map: null};MyFirstGame.Load = function (game) { this.game = game;};MyFirstGame.Load.prototype = { preload: function () { // load assets this.load.image('menuBackground', 'assets/loadingBackground.png'); },};And reference these vars like this: MyFirstGame.level, MyFirstGame.player, etc I was (sort of unintentionally) adding my global vars as properties of Phaser.Game until I saw this post.. so thanks Rich! Link to comment Share on other sites More sharing options...
Igor Georgiev Posted June 15, 2016 Share Posted June 15, 2016 On 9/11/2014 at 4:43 PM, JUL said: "Nothing: Phaser will destroy it? Phaser destroys all objects added to game with this.game.add?" Phaser is supposed to destroy everything in the state if you switch state, yes. As long as all object are created within the sate as I understand it. Now depends on how you coded the whole thing, if your approach was rather orthodox or totally heterodox. The later might produce unexpected results. Man, I tried that and if I leave the the game without background in the game state, the background and elements from the previous state are still there.... I did a debug, and the sprites from the previous state says it is a Phaser.Sprite.... Link to comment Share on other sites More sharing options...
Igor Georgiev Posted June 15, 2016 Share Posted June 15, 2016 7 minutes ago, Igor Georgiev said: Man, I tried that and if I leave the the game without background in the game state, the background and elements from the previous state are still there.... I did a debug, and the sprites from the previous state says it is a Phaser.Sprite.... I think I found the reason: game.autoResize = true; game.forceSingleUpdate = true; game.preserveDrawingBuffer = true; game.clearBeforeRender = false; game.lockRender = false; those options seem to leave an artifact from the previous sprites. Link to comment Share on other sites More sharing options...
rich Posted June 15, 2016 Share Posted June 15, 2016 Those options will not leave Sprites objects themselves behind, only the image of them. If when you change state you tell it to clear the display list, then it'll clear it, regardless of those options. Link to comment Share on other sites More sharing options...
Igor Georgiev Posted June 15, 2016 Share Posted June 15, 2016 26 minutes ago, rich said: Those options will not leave Sprites objects themselves behind, only the image of them. If when you change state you tell it to clear the display list, then it'll clear it, regardless of those options. I would also suppose so, however, with those options they stay behind as images, as you said. Also can I ask you something as master of Phaser ? What kind of fps would you expect from iphone 4 with game running at native resolution? (running through safari on iOS 9.3.1, phaser is 2.4.7 no-physics) Link to comment Share on other sites More sharing options...
rich Posted June 15, 2016 Share Posted June 15, 2016 If the drawing buffer is preserved, then yes, you'll need to clear it down yourself. This is what the flag is for. The actual Sprite objects however will have been removed from the display list (unless set otherwise) Your second question is unanswerable I'm afraid. Link to comment Share on other sites More sharing options...
Igor Georgiev Posted June 15, 2016 Share Posted June 15, 2016 23 minutes ago, rich said: If the drawing buffer is preserved, then yes, you'll need to clear it down yourself. This is what the flag is for. The actual Sprite objects however will have been removed from the display list (unless set otherwise) Your second question is unanswerable I'm afraid. Okay, I get it. Thank you for the attention Link to comment Share on other sites More sharing options...
Recommended Posts