Jump to content

Phaser why do I have to use destroy insted of kill


PhasedEvolution
 Share

Recommended Posts

What do you mean by kill() doesn't actually kill the sprite? It should remove it from view at least.

You can use either kill or destroy if you want to. You can destroy everything if you want, and you can still make a working game. The reason you would use kill is because in many cases it will be more efficient.

Are you familiar with memory allocation or garbage collection concepts? It would take too long for me to explain it here, but if you don't know how they work, it may be worth doing some googling to research it.
The basic idea is creating a new object causes Phaser to do some time consuming work that is hidden from you. If you revive a killed object, you get to skip that expensive step. If you destroy the object and then create a new one in its place, that expensive step has to happen again.

It is especially important for games that have objects that are being removed and added often: things like bullets, constantly spawning enemies, pick up items like coins and power ups, etc.  If you want to remove a sprite and never use it again, then it is best to destroy it, but if you plan on re-using it, it is a good idea to get comfortable with kill/revive now. If you design the game to use destroy now and then find out that the game is too slow later on, it can be very difficult to refactor the code to use kill/revive in place of destroy.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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