HTML5 Endless SameGame engine with object pooling made with Phaser

This is an important update to the SameGame series because I making the game endless, allowing you to create most of the games mentioned in the post 10 successful games you can easily create starting from the SameGame engine and I am using object pooling to handle tiles management.

Object pooling is a technique which stores a collection of a particular object that an application will create and keep on hand for those situations where creating each instance is expensive.

In this case, each time you pick and remove tiles, their sprites should be destroyed. Then, new sprites should be created to replace the ones you just destroyed. That is, if you make a four tiles combo, you have to destroy the 4 old sprites and create 4 new sprites. Then you make a 11 tiles combo. This means 11 sprites destroyed and 11 new sprites to create.

Although I am sure Phaser has a good memory management and garbage collection, this can be very resource-consuming in the long run. So the idea is never to delete removed tiles, which will be temporarily stored in a repository (in this case an array) until a new tile is needed, and we just recover the previously stored tile.

This also happens when you buy a pizza at a restaurant, you complain because it’s not tasty, and they pretend to change your bad pizza with a new one but they just serve you the same pizza again. It happens. No doubt.

Anyway, this is the game we are going to create:

Select a tile with at least another tile of the same color around it, and see what happens. No sprites are destroyed. It’s just object pooling.

Here is the source code:

Using object pooling whenever possible will dramatically decrease the resources used by your game. Download the source code of the entire project and have a go.