Author Topic: Kill all the members in a group?  (Read 1740 times)

Zapleaf

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Kill all the members in a group?
« on: Sat, Sep 27, 2014 »
I have tried several attempts but my code keeps causing a lot of issues.  I simply want to remove every member in a group but keep the group itself a live.

Code: [Select]
if ( worlds.members.length > 0 )
return;

Edit...  half the post was removed?  so.. here's a quick rundown:  How do I kill just the members of the FlxGroup without killing the Group itself. And no this is not something you can google.

Edit: When I use this function I made:
Code: [Select]
public function killMemebers():void
{
if ( GalaxyBackground )
GalaxyBackground.kill();
if ( WorldHolder.length > 0 )
{
for ( var index:int = 0; index < WorldHolder.length; index++ )
WorldHolder[index].kill();
}
}
I still produce a members.lenger greater than zero when checking members.length, even though everything is removed from the screen.
« Last Edit: Sat, Sep 27, 2014 by Zapleaf »

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Kill all the members in a group?
« Reply #1 on: Sat, Sep 27, 2014 »
Killing doesn't remove them from the group. It "kills" them, but they sit around in the group so you can recycle and reuse them rather than continuously creating new objects.

To completely get rid of them you can do

Code: [Select]
group.clear();
To remove everything. But if you have other references, or things that happen when killed, you should loop over and call kill() on everything (there's a group.callAll that can help with that).

Zapleaf

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Kill all the members in a group?
« Reply #2 on: Sat, Sep 27, 2014 »
Killing doesn't remove them from the group. It "kills" them, but they sit around in the group so you can recycle and reuse them rather than continuously creating new objects.

To completely get rid of them you can do

Code: [Select]
group.clear();
To remove everything. But if you have other references, or things that happen when killed, you should loop over and call kill() on everything (there's a group.callAll that can help with that).

I must have looked over these functions when skimming though all the options I had, there are quite a lot of them.  Thanks!

Edit:  I forgot to mention I came up with a solution before you replied.
Code: [Select]
Layer0.remove(_Map, true).kill();
What I did was create a flxGroup named Layer0 and put everything inside of it.  When I want to remove the stuff I call that line of functions.  And for logic i do a test on all the members of that layer.  Is this an okay of going about it?
« Last Edit: Sat, Sep 27, 2014 by Zapleaf »

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Kill all the members in a group?
« Reply #3 on: Sun, Sep 28, 2014 »
I must have looked over these functions when skimming though all the options I had, there are quite a lot of them.  Thanks!

Edit:  I forgot to mention I came up with a solution before you replied.
Code: [Select]
Layer0.remove(_Map, true).kill();
What I did was create a flxGroup named Layer0 and put everything inside of it.  When I want to remove the stuff I call that line of functions.  And for logic i do a test on all the members of that layer.  Is this an okay of going about it?

It will work, but it'll be extremely slow. Just calling remove once is going to be many times slower than calling .clear(), so calling remove on everything is super slow (it does a search in the group to find what you're removing, and then deletes it, then moves everything after it in the list over to fill in the space).

Doing

Code: [Select]
Layer0.callAll("kill");
Layer0.clear();

Would be a lot faster. Though depending on how big the group gets and how often you do it, it might not matter.

Zapleaf

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Kill all the members in a group?
« Reply #4 on: Sun, Sep 28, 2014 »
Code: [Select]
Layer0.callAll("kill");
Layer0.clear();

Would be a lot faster. Though depending on how big the group gets and how often you do it, it might not matter.

The purpose I have it for currently is just for a few sprite images, but seeing as this is all new to me, it would probably be best to start good habits.  Thank you very much for the help!