Flixel Power Tools FlxCollision

Back to the Flixel Power Tools

Collision in flixel is handled with bounding boxes. Which is basically two rectangles colliding, and if they intersect you get a collision back. This class takes it the necessary step further and adds pixel perfect collision testing. Only the intersecting area is tested for speed. And you have control over the alpha tolerance level (so you can exclude pixels with an alpha level less than what you need). It also works with scaled, rotated or animated FlxSprites! and is perfectly fast enough to use in real-time.

Screen Shot

Code Example

Recent Flixel Power Tool Posts

  1. Flixel Power Tools v1.9 Released
  2. Flixel Power Tools v1.8 Released - Let's get clicky
  3. Flixel Power Tools v1.7 - Kaboom!
  4. Flixel Power Tools v1.6 released including FlxControl
  5. Flixel Power Tools v1.5 - A monster of an update!
  6. FlxScreenGrab and FlxScrollZone added to Flixel Power Tools
  7. Flixel Power Tools v1.3 - Now Flixel 2.5 compatible!
  8. FlxHealthBar added to Flixel Power Tools

18 Responses

Leave a comment
  • 400
    July 15th 2011 at 12:18 am

    hello ps,

    thanks for this great tools.

    could you give me some directions on how to implement something
    like FlxCollision.pixelPerfectCheck(level, player) – where level is a tilemap
    built in dame (basically a FlxTilemap).

    i tried:
    if (FlxCollision.pixelPerfectCheck(level1 as FlxSprite, _ship as FlxSprite))
    and get:
    [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.

    any ideas?

    thanks, 400

  • July 15th 2011 at 12:41 am

    Sorry but what you’re trying to do just isn’t possible using this class. Not without a lot of work anyway – Flixel tilemap collision is all bounding box based, which is why things like slopes and angled surfaces are so hard. I’ve been meaning to look into it, but haven’t done so yet.

  • 400
    July 15th 2011 at 12:27 pm

    thanks for your reply.

    have you seen this:

    would it be possible to retrieve the colliding tile of the standard flixel collision as a FlxSprite, ignore the collide and just pass it to FlxCollision.pPC?

    im new to flixel and just woke up…


  • July 15th 2011 at 1:03 pm

    Yes that’s basically how you’d have to do it, but you’d still really need the angle and depth of collision in order to perform the separation (which FlxCollision doesn’t provide yet)

  • August 28th 2011 at 6:12 pm

    Hey – nothing intelligent to say, just very pleased to make the acquaintance of your library – in this case particularly because of the pixel-perfect stuff, which I needed and *really* didn’t want to implement. The library in general looks lovely, too – great work!

  • Wilfred Ong
    August 30th 2011 at 1:46 pm

    can I check it with a flxGroup?

  • August 30th 2011 at 1:59 pm

    Wilfred – You can’t pass in an FlxGroup I’m afraid. This isn’t really a replacement for FlxG.collide (which of course can work with groups) but more of a “next step”, so once you’ve got a bounding box collision between 2 objects, you can use this to determine if it’s a pixel-perfect one or not and respond accordingly.

  • keenblaze
    September 10th 2011 at 3:36 pm

    Thanks for this useful class! Unfortunately, it doesn’t work with scaled images… :( (Maybe I’m doing something wrong?)

  • Glycerin
    October 26th 2011 at 4:32 pm

    I have a problem with the collision. Ik have a Player with an axe and an enemy. The axe is rotatable with my mouse and i want to check if the axe hits the enemy. But if its rotated it won’t detect it. I use trace and it only works if i walk over him with my axe, not if u rotate my axe infront of me and hit him.

  • October 26th 2011 at 4:36 pm

    If you’re using v1.9 of the tools it works with rotated sprites quite happily, and there is an example in the test suite. I assume you’re checking just the axe vs. another object, and not the whole player?

  • December 12th 2011 at 8:44 am

    It doesn’t seem to work with scaled images.

  • John Doe
    February 14th 2012 at 1:43 pm

    As far as I can tell from looking at Flixel internals – pixel perfect check doesn’t work with scaled images, because Flixel doesn’t change original bitmapData. The result: after scaling the image, pixel perfect check is made against old alpha channel data.

    As a quick and dirty solution (to improve upon) to this problem I created a new bitmapData using “pixels” from my FlxSprite, resized it, and assigned back to sprite’s “pixels”.


    Disclaimer: I’m a crappy coder. And really – I’m not a coder at all. Everything mentioned in this post is a pure guess and I could be damn wrong.

  • Jack
    May 8th 2012 at 5:45 pm

    “If you’re using v1.9 of the tools it works with rotated sprites quite happily”

    But only if you loaded the bitmap using loadRotatedGraphic, right?

  • Joe
    September 6th 2012 at 10:44 pm

    Hi guys, first let me thank you for developing these tools, they are great. I’m developing my first game and I’m having a problem using some of the classes, like Flx Collision, I’m getting this error:

    Error: Implicit coercion of a value of type org.flixel.plugin.photonstorm:FlxCollision to an unrelated type org.flixel:FlxBasic.

    Here is my code to implement to FlxCollision:

    if (FlxG.getPlugin(FlxCollision) == null) {
    FlxG.addPlugin(new FlxCollision);

    Is there any solution, am I doing something wrong?
    Thanks in advanced

  • September 6th 2012 at 11:56 pm

    Hi Joe – FlxCollision can be used directly. Just import the class and call any function you like on it, you don’t need to add it as a plugin (only some of the classes require that – those that extend FlxBasic, like FlxScreenCapture)

  • zzzz
    February 14th 2013 at 9:14 pm

    Hi guys, I have a question, when FlxSprite rotates, “framePixels.width” and “framePixels.height” didn’t change, this led to the collision detection errors!

    “FlxCollision::pixelPerfectCheck” – code:
    var boundsA:Rectangle = new Rectangle(pointA.x, pointA.y, contact.framePixels.width, contact.framePixels.height);
    var boundsB:Rectangle = new Rectangle(pointB.x, pointB.y, target.framePixels.width, target.framePixels.height);

    my english is poor, can you understand it? Help!! Thanks!!

  • zzzz
    February 16th 2013 at 1:15 pm

    FlxCollision don’t support “loadGraphic”?!

  • Brandon Miller
    April 22nd 2013 at 7:19 pm

    For those of you who want to use FlxCollide.pixelPerfectCheck() on FlxGroup’s as well, here is a function for you. I takes an array of FlxSprite’s as each target, so to check against an FlxGroup, just pass in “yourFlxGroup.members”. Or to check single sprites, just pass them as a single item array “[mySprite]”.

    function checkCollide(group1:Array, group2:Array, callback:Function) : void {
    // Loop through all objects in the groups.
    for each (var target1:FlxSprite in group1) {
    // Only check for collision if the object’s in use.
    if (!target1.alive) continue;

    for each (var target2:FlxSprite in group2) {
    if (!target2.alive) continue;

    if (FlxCollision.pixelPerfectCheck(target1, target2, 1)) {
    // Pass the objects to the notification callback.
    callback(target1, target2);

    Example Usage:

    // The objects to test for collision.
    var mySprite:FlxSprite = new FlxSprite();
    var myGroup:FlxGroup = new FlxGroup();

    // The function called if the objects collide.
    function onCollide(obj1:FlxSprite, obj2.FlxSprite) : void {
    // Destroy both objects if they collide.

    // Use this to call the callback if ‘mySprite’ collides with anything in ‘myGroup’.
    checkCollide([mySprite], myGroup.members, onCollide);

Make yourself heard