Flash Game Dev Tip #3 – Bullet Manager Part 1

Tip #3 – Flixel – Bullet Manager Part 1

If you are coding a shoot-em-up, or even a platformer with guns, then you’ll have a need for the player to be able to fire bullets. Or the enemies to fire at you. This tip is about creating a Bullet Manager class. The class is responsible for the launch, update, pooling and re-use of bullets.

Object Pool

Creating new objects in Flash is expensive. By “new objects” I mean code such as:

… which creates a brand new object an assigns it to bullet.

And by “expensive” I mean it takes time for Flash to process the request for the new object, assign memory to it and create it. If you are firing off tens of bullets every few seconds this can take its toll. And if you don’t actively clean-up the objects created you can quickly run out of resources.

To mitigate this problem we create a “pool”. This is a pool of resources (in our case bullets) that the Bullet Manager can dip in to. It will look for a free bullet, and recycle it for use in the game. When the bullet has finished doing what bullets do best, it will free itself up for use again. By using a pool you avoid creating new objects on the fly, and help keep memory in check.

Meet FlxGroup

Thankfully flixel has a class you can use to make this process simple. It’s called FlxGroup. You can add objects to a group, there are plenty of  functions for getting the next available resource, and you can even perform group to group collision. Objects in a group are all rendered on the same layer, so are easy to position within your game. The first task is to create a pool of bullets to draw from.

In this example we’ve got a class called Bullet.as. Bullet extends FlxSprite with a few extra values such as damage and bullet type.

The important part is that the bullet sets exists to false when created to make it immediately available for use. When fired the bullet will travel up the screen. The check in the update method simply sets the bullet to not exist once it has passed y 0 (the top of our screen).

So far, so simple. Next up is the BulletManager.as class. This extends FlxGroup. It begins by creating a pool of 40 bullet objects and adding them to the group ready for use. As all of them have exists equal to false none will render yet.

It has one extra method called fire. This gets the first available bullet from the pool using getFirstAvail (the first bullet with exists equal to false) and then launches it from the given x/y coordinates by calling the bullets fire function.

Are you feeling lucky?

I insert the Bullet Manager into the games Registry (see Tip #1 if you don’t know what the Registry is) so my Player class has easy access to it. My Player class is a simple sprite with keyboard controls to move it around. When you press CTRL it will fire a bullet:

The Bullet Manager handles the request and launches a bullet up the screen. The +5 after the x value is just to visually align the bullet with the middle of the space ship, otherwise it’d appear off to the left.

At the beginning of the Bullet Manager class I hard-coded in a limit of 40 bullets. That is enough for my game. As you can see in the screen shot above I’m only using 32 bullets out of a pool size of 40.

This value may not be suitable for your game. I don’t know what value you need, only you do. Perhaps you are coding the next Ikaruga, in which case you probably need 40 bullets per pixel :) The thing is, you can tweak this as needed. And you can tweak it in-game too. It’s not a value that should change dynamically, but it could easily change from level to level as the game progresses.

Download

This example is about as simple as I could make it, but hopefully you can see the benefits already. In the next tip I’ll add group collision detection and something for you to shoot at.

Download the source code and SWF from the new Google Code Project page.

Posted on February 25th 2011 at 6:10 pm by .
View more posts in Flash Game Dev Tips. Follow responses via the RSS 2.0 feed.


10 Responses

Leave a comment
  • mythril225
    February 27th 2011 at 9:18 am

    Thanks for posting this, i find it very useful and it’s good that you’re
    giving tips on how to code in an organized and clean manner.

  • Mark
    February 27th 2011 at 11:15 am

    How does the getFirstAvail() function work? Does this cycle through the bullets to find a match, or does it use some other actionscript?

    Is this better on performance than creating a new bullet each time?

  • February 27th 2011 at 12:36 pm

    Mark – in flixel objects have an exists boolean value. If they are exists = false they don’t render, and are considered “available for use” by an FlxGroup. So getFirstAvail() loops through the members of the group, finds the first one available for use and then stops at that point. So yes, definitely more efficient than creating loads of new objects per frame.

  • Mark
    February 27th 2011 at 8:42 pm

    Wouldn’t it be more efficient just to call getFirstAvail once in the example code and then check the resultant rather then run it again/

  • February 27th 2011 at 9:45 pm

    Nothing is stopping you changing it to do that.

  • MC
    March 11th 2011 at 9:58 pm

    There’s a little bug… if you hold the fire button you can’t move left/right anymore

  • Baelic
    October 21st 2011 at 3:56 am

    Hey there, any idea what I would have to do in order to switch between 2 different bullets?

    I’ve been using these examples to get my project started but I want to expand it so there’s different types of weapons the player can choose from and fire. Any help would be awesome!

  • October 21st 2011 at 9:59 am

    In the demo above F1, F2 and F3 swap between different weapons (basically more or less bullets are fired). There’s no reason why the keys couldn’t swap to a different type of bullet entirely though.

  • Nick
    March 19th 2013 at 1:58 am

    You guys are Awesome, I can not thank you enough for the Flixel Power Tools!

    Thanks!

    Nick

Make yourself heard