Search code examples
haxehaxeflixel

How do I identify specific entity within a FlxGroup from FlxG.collide?


How do I make it so that when a bullet from the bullet group collides with an enemy from the enemy group, only the two hitting eachother will get affected?

I tried doing (In playstate):

if (FlxG.collide(bullet, enemy)){
        bullet.kill();
        enemy.kill();
    }

But the only thing this succeeded in doing is killing the entire group. How do I only kill the ones affected?


Solution

  • In the Haxeflixel API docs:

    collide(?ObjectOrGroup1:FlxBasic, ?ObjectOrGroup2:FlxBasic, ?NotifyCallback:Dynamic‑>Dynamic‑>Void):Bool
    

    so I think you can use something like:

    FlxG.collide(
        groupBullets, 
        groupEnemies, 
        function (bullet:FlxObject, enemy:FlxObject):Void {
            enemy.kill();
            bullet.kill();
        }
    );