Do I need to kill all sprites before switching states in Phaser?
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.
source to share
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.
source to share
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);
};
source to share