Making a custom pool class by extending Phaser.Group.

Article by Richard Davey. Posted on 11th Mar 2016.   @phaser_

image

This new tutorial is by Niklas Berg, author of the Metroid remake: "Creating and destroying objects are quite expensive. Memory needs to be allocated for new objects, and their destruction will trigger the automatic garbage collector that may cause spikes in CPU usage. This might not be a problem if you’re creating a something like a board game. However, would you repeatedly create and destroy bullet sprite objects in a shoot ’em up with massive amounts of bullets, it would certainly affect the performance. The solution is called pools. Instead of destroying the objects, you just stuff it away until an object of the same type is needed again.

The Phaser.Group class can be used for various purposes like bulk actions (i.e. enabling gravity on all bodies in the group). It can also be used for pools. In the Metroid Remake, I use a custom pool class which is an extended Phaser.Group. All sprites, except Samus, is pooled. When an enemy is destroyed, its exists-property is just set to false, and when I need an enemy of the same kind I reset stuff like the health property and then I set the exists-property to true again."

Read More