Jump to content

Can someone help explain Groups.RETURN_ALL?


Recommended Posts

The functionality behind the groups RETURN_ALL doesnt seem to be working the way that I was expecting it to.

Lets say I have two groups, each represents an army, but I want to pull out certain soldiers from the groups to do combat based on their location.

group1 = player1_army.iterate('location','PLACE1',Phaser.Group.RETURN_ALL);
group2 = player2_army.iterate('location','PLACE2',Phaser.Group.RETURN_ALL);

I was expecting group1 and group2 to be actual groups, but they dont seem to be.
This doesnt work:


But this does work:


Also, if destroy an element from group1, it does actually destroy the sprite from player1_army , but the length of group1 doesnt decrease, it just stays in group1 as if nothing has changed.

Based on what I can find in the documentation, is group1 just an array of sprites and not an actual group? If i wanted for an element to be removed from group1 after I destroy , do I need to do something like group1[-0] or something?

Looking to understand how this is working, thanks!


Link to comment
Share on other sites

results1 = player1_army.iterate('location', 'PLACE1', Phaser.Group.RETURN_ALL);
results2 = player2_army.iterate('location', 'PLACE2', Phaser.Group.RETURN_ALL);

RETURN_ALL returns all the matching items in a plain array. It doesn't create a new Group or move the sprites from their current Group. You would have to splice the array if you wanted to update its length.

If you want to create new groups, you would do 

var group1 = this.add.group();
var group2 = this.add.group();

player1_army.moveAll(player1_army.iterate(/* … */), group1);
player1_army.moveAll(player1_army.iterate(/* … */), group2);


Link to comment
Share on other sites


  • Recently Browsing   0 members

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