Author Topic: Custom FlxGroup class as callback parameter for overlap()?  (Read 915 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
I'm trying to use the FlxU.overlap() function to check collision between my melee sprite and a group containing my enemies.

My enemies are made of classes called EnemyEntity which extends FlxGroup and has a sprite inside for the graphic. I put all of these in another group called enemies in the playstate.

When I go to check for overlap via FlxU.overlap(melee, enemies, enemyHit), the game freezes up when it uses the callback function shown here:

Code: [Select]
public function enemyHit(_melee:FlxObject, enemy:EnemyEntity):void {}
I've identified the reason being that I'm using the EnemyEntity class (which extends FlxGroup) as a parameter, for if I change the parameter to enemy:FlxObject it works fine.

The problem is, I need the object passed in to be an EnemyEntity so I can access some public variables and functions necessary to have the enemy be hit and damaged.

Is it not possible to use custom classes as parameters in overlap() callback functions? If so, any suggestions on what I'm doing wrong?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Chipacabra

  • Member
  • **
  • Posts: 63
  • Karma: +0/-0
    • View Profile
    • No Idea What I'm Doing
It's always going to send whatever object it gets at the callback, and if the type doesn't match up it'll throw a fit.
The way to deal with this is to have
Code: [Select]
public function enemyHit(_melee:FlxObject, enemy:FlxObject):void
{
   if (enemy is EnemyEntity) { EnemyEntity(enemy).myFunction(); }
}

The if check probably isn't necessary if you know for sure that you're getting an enemyentity.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Thank you for that suggestion, it helped me realize that it's overlapping with and passing the sprite contained inside of the EnemyEntity class. I'm dumb :-[

Thanks
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery