Do I need to kill all sprites before switching states in Phaser?

Do I need to kill all sprites and animations before switching from state A to state B in Phaser, or will Phaser automatically clear them?

+3


source to share


4 answers


Kamen Minkov's answer is almost right, but the argument you need to consider is clearWorld

.

If you set it to false, all of your objects will stay there when you toggle the state, a bit like you set both states at the same time. Otherwise, the default behavior should really destroy all of your game objects when switching states.



clearCache

is a cleanup of preloaded assets (this means you have to preload them again). Most of the time, you will want to leave it false, unless, for example, you have downloaded a large number of cinematic assets and you no longer need them; in this case, deleting them from the cache is probably a good idea to free up memory.

+1


source


Here the clearCache

bool parameter is for Phaser.StateManager.start()

(third), so you most likely don't need to do anything manually.



API docs for StateManager

0


source


Kill doesn't really mean cleanup like when cleaning up memory. This has nothing to do with removing the sprite from the game, it's just declaring the sprite to be killed and removed from the view. I believe Phaser actually handles the cleanup for you automatically when you switch between states.

0


source


You must free the memory used by your game state in a method shutodown

that is automatically called by Phaser when states change.

Example:

MyState.prototype.shutdown = function () 
{
     this.background.destroy();    //Phaser.Image
     this.mySprite.destroy(true);  //Phaser.Sprite
     this.myImage.destroy(true);   //Phaser.Image
     this.game.cache.removeImage("image-I-wont-use-anymore", true);
};
      

Run codeHide result


0


source







All Articles