Author Topic: Change FlxGroup Member properties  (Read 1691 times)

The Pink Bunny

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Change FlxGroup Member properties
« on: Mon, Apr 20, 2015 »
I have a FlxGroup filled with FlxSprites, and I want to be able to change the velocity of all of the sprites so that they move together. Is there a way to change the velocity property of all of the members of this group without having to loop through the group or write a separate line to change each of them?

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Change FlxGroup Member properties
« Reply #1 on: Mon, Apr 20, 2015 »
Yes, you can do something like this in the playstate:

_spritegroup.members.forEach(VelocityChangeFunction);

Another method would be to use static variables to communicate between the sprites, but that's a bit more complicated I think.

The Pink Bunny

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Change FlxGroup Member properties
« Reply #2 on: Mon, Apr 20, 2015 »
Thanks for the help! Does forEach() do anything that setAll() couldn't do? (I just happened to see it in the FlxGroup documentation)

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Change FlxGroup Member properties
« Reply #3 on: Mon, Apr 20, 2015 »
I'm not sure. I haven't used setAll (not in the version of Flixel I've been using I think). With forEach you can run a function for all the members of your FlxGroup which gives you an enormous amount of flexibility since you can put conditionals in it.

The basic form of that function could be something like:

Code: [Select]
private function EnemiesFallBack(element:*, index:int, arr:Array):void
{
   if(!element.disabled) //public boolean "disabled" for enemies to indicate if player has whammied them
      {
         if(_player.x > element.x) element.velocity.x = -100;
         else element.velocity.x = 100;

         if(_player.y > element.y) element.velocity.y = -100;
         else element.velocity.y = 100;   
      }
}

A function like that would tell all your enemies to retreat from the player, but their motion would be based on their relative position to the player - and they'd only do it if they weren't disabled by the player.