jun
jun

Reputation: 33

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.

Upvotes: 2

Views: 458

Answers (1)

Gama11
Gama11

Reputation: 34148

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
});

Upvotes: 3

Related Questions