HaxeFlixel Study – FlxTeroids, Part 3: Code Study

If you study the FlxTeroids demo project closely, you can learn a lot of things from it. But just in case you’re completely new and would like some interesting points highlighted, do read on.


Note that the t  variable is a reference rather than the value itself. As such you can reuse variables when adding objects of the same type into the stage.


Note how easy it is to create a simple star field in HaxeFlixel! 😀

The FlxTypedGroup  is similar to an Array, except its purpose can be similar to an Object Pool — you can use recycle()  to reuse inactive objects in the group. Check out the API for more info.

There are two ways to check for collisions in HaxeFlixel — overlap()  and collide() .

overlap()  will trigger when the two objects intersect, but you will have to handle collision the logic yourself.

collide()  will trigger when two objects intersect as well, but it includes physics; The collided objects will push away from each other automatically. You can use this for situations where you know two objects must not stay intersected with one another, such as platformers (between the player and floor).

Also note that even though the API accepts two arguments, you can just provide one instead, as shown in the code  FlxG.collide(asteroids); . This is because asteroids is a FlxTypedGroup , and collide()  will check for collision among all the objects within the group. Very convenient!

Also note that you can overlap or collide two FlxTypedGroups , and it’ll automatically trigger collision between objects from the two groups.

The screenWrap()  function is very convenient. Check the API for more info.

The FlxTween library is convenient for various animations. You might end up using it on many occasions 😀

The kill()  function is part of FlxBasic that can be conveniently used to disable a game object. You can later then use reset()  or revive()  to reenable the game object.

In the case of FlxTeroids — when asteroids are killed, you can instead use recycle()  to revive them. Note that we pass the Asteroid class as parameter into the recycle()  function. This is so that if there are no available inactive asteroids in the group, it will create and add a new asteroid into the group.

You may need FlxTimer  on several occasions, such as “spawn enemy every X seconds”, so take note of its existence 🙂 Check out the API for more info.

We shall continue studying the code in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *