haxehaxeflixel

Is there another way to checking collision without FlxG.collide?


I'm making a game with Haxe and the HaxeFlixel game engine, and I'm implementing collisions.

I found a way to check for collisions in the HaxeFlixel cheat sheet:

FlxG.overlap(ObjectOrGroup1, ObjectOrGroup2, myCallback);

private function myCallback(Object1:FlxObject, Object2:FlxObject):Void
{
}

But I don't like this style because the callback is separated from the function call. I would prefer this:

FlxG.overlap(ObjectOrGroup1, ObjectOrgroup2, function() {
    //do something
});

I saw this style in JavaScript, but I don't know exactly what it is. Is this possible in Haxe?

And I need this code like this:

if (FlxG.collide(Object1, Object2)) {
    //do something
}

It must return the value (true or false) and it processed by if statement.


Solution

  • Your code snippet for the anonymous function is almost correct, but the callback function for FlxG.overlap() expects two arguments (the objects that collided). Try this:

    FlxG.overlap(ObjectOrGroup1, ObjectOrGroup2, function(object1:FlxObject, object2:FlxObject) {
        // do something with object1 and object2
    });