Jump to content

Getting elements from Group


Draconar
 Share

Recommended Posts

How do I pick specific members of a group?

 

My naive solution was:

 

  • create all elements and add them to the group (basic for loop)
  • at the same step, add the created element to an Array variable
  • get specific members at play time by their Array's reference

 

Besides that, how do I change a member's group? For example, there is a hockey team group and there is a Player+ball group, any member of Hockey's team can become a Player+ball group.

 

 

 

 

Link to comment
Share on other sites

If you want to target specific members of a group, I believe you'll have to iterate through all of them.

 

For example:

group.forEach(function(member, param1) {	if(param1 > 50 && member.hp < 250) {		// do stuff	}}, this, true, param1);

I think Phaser objects that can be in a group aren't restricted to just that group.

 

But if you later want to get the same targets again later, it's probably better for performance to add them in a new group, rather than iterate through the main group over and over again.

 

And if you want to remove a member from a group, do something like

group.remove(member);
Link to comment
Share on other sites

FWIW, Phaser objects can only be in one group at a time. Adding a Phaser object to a new group will remove it from the old group.

 

This is great. Exactly what I need. I want to remove a member from a group and insert it into another. How can I do that?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...