Flixel Forums

development => help => Topic started by: Esti on Thu, Feb 9, 2012

Title: How do I call a function of an Object that is inside a Group?
Post by: Esti on Thu, Feb 9, 2012
I add my enemies(enemy class) into a group(enemies) and I want to use overlap to call a function that is inside the enemy class.
Quote
FlxG.overlap(player, enemies, posesion);

then

Quote
protected function posesion(Sprite1:FlxSprite, Sprite2:FlxSprite):void
      {
      
      if (Sprite1 is Player && Sprite2 is enemy)
      {
      
              //call a function here like enemies(Sprite2).doSomething();
      
      }

Is there a way to do this? Thank you.
Title: Re: How do I call a function of an Object that is inside a Group?
Post by: Seagaia on Sat, Feb 11, 2012
assuming enemies is a FlxGroup with Enemy objects inside -


Say we have

FlxG.overlap(player, enemies, possession);

Then when player and any enemy in the enemies group overlap, the following callback would go off with that specific enemy. Notice the order of the arguments in the callback function, possession, and the order of the first two arguments to FlxG.overlap(..).

public function possession(player:Player,enemy:Enemy) {
  ...your code here...
   enemy.doWhatever();
   player.doWhatever();
}
Title: Re: How do I call a function of an Object that is inside a Group?
Post by: Esti on Sat, Feb 11, 2012
I get it now, Thank you!.   :-*

The problem was my Posesion function recieved 2 parameters that were "Sprite" type so if wanted to call a specific function of the enemy asociated to that sprite I wouln't know how. I changed the parameters to Enemy and Player now.

Funny fact:  is "posesion" because it is in spanish  ;D

I can now continue with my ghost-possessive thing I've here.