Author Topic: How do I call a function of an Object that is inside a Group?  (Read 1073 times)

Esti

  • Active Member
  • ***
  • Posts: 104
  • Karma: +0/-0
    • View Profile
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.

Check it out on Mochi!

Seagaia

  • happens to be a
  • Active Member
  • ***
  • Posts: 112
  • Karma: +0/-0
  • Howdy
    • View Profile
    • twitter
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();
}
making anodyne, a zelda-like (with flixel!)
anodynegame.com

Esti

  • Active Member
  • ***
  • Posts: 104
  • Karma: +0/-0
    • View Profile
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.

Check it out on Mochi!