Hey everyone, today we talk about Pooling. Object pooling is a way to optimize your game that you should be using right now regardless of what game engine you’re using. Now, you might be wondering what Object Pooling is. Don’t worry, I’ll give you the details right now.

Object Pooling Concept

Let’s say you’re making your first bullet hell game. You’re firing bullets in every direction. Your player pew pews the enemies. Awesome!! But, then suddenly the game starts lagging. Why? Well, because the bullets you spawn never disappear from the game. So, every time you fire, the game is just adding more and more stuff to the PC memory, slowing down the game.

Object pooling is the solution to that problem. Object pooling is when you take the objects like the bullets that you spawn and reuse them rather than creating more. This saves space, optimizes memory and more.

So, how do we achieve this in HaxeFlixel? The actual code is straight-forward. Here’s how you setup pooling in HaxeFlixel!

import flixel.FlxState;
import flixel.group.FlxGroup;
import flixel.FlxSprite;
import flixel.util.FlxColor;
class PlayState extends FlxState {
//bullet group for holding player bullets
public var playerBulletGrp:FlxTypedGroup<FlxSprite>;
public static inline var PLAYER_BULLET_CAP:Int = 20;
override public function create() {
//Total amount of bullets the player can always have.
//In this case the player can only have 20 bullets.
playerBulletGrp = new FlxTypedGroup<FlxSprite>(PLAYER_BULLET_CAP);
override public function update(elapsed:Float) {
var fire = FlxG.keys.anyJustPressed([Z]);
if(fire) {
//Fire a bullet if the player hits the Z button
var bullet = playerBulletGrp.recycle(FlxSprite);
bullet.makeGraphic(4, 4, FlxColor.WHITE);
bullet.setPosition(100, 50);
bullet.velocity.set(300, 0);
for(bullet in playerBulletGrp) {
//Kills the bullet when it flys off screen so it can be reused later (object pooling)
if(!bullet.isOnScreen()) {

As you can see all we do is cap the amount of elements that are in the group. Additionally, all you need to do to spawn objects is to use the recycle method above. Bam!! That’s pooling in HaxeFlixel.

For a live example, you can find it here. Play around with the size and see what happens!


With that said, I hope the post was helpful! Enjoy and good luck game making!! Stay tuned next time where we will get into more awesome tools in HaxeFlixel!

%d bloggers like this: