Jump to content

Group vs Group in Phaser


spinnerbox
 Share

Recommended Posts

As shown in this topic: 

 

It seems, separate groups are interleaved. How do they function with the z-index between each other?

If I create group1 first and group2, group1 is below group2 right?

What if I add element1 first to group2 and then element2 to group1 will element1 be above element2 since group2 was created later or its vice versa?

Can somebody explain me how Phaser groups work?

Link to comment
Share on other sites

Hi, when you are adding images, sprites and other game objects into groups and world, you are in fact building scene graph. Root is world and groups are containers for other game objects. This scene graph looks like tree and it determines rendering order.

Quote

If I create group1 first and group2, group1 is below group2 right?

Yes - scene tree looks like:

world
  ˪ group1
  ˪ group2

Quote

What if I add element1 first to group2 and then element2 to group1 will element1 be above element2 since group2 was created later or its vice versa?

world
  ˪ group1
      ˪ element2
  ˪ group2
      ˪ element1

 element1 will be on top.

 In engines like Unity you can affect rendering order by setting Z coordinate across whole hierarchy. In Phaser you can set Z order only within single group. It will reorder children in that group. You can also change order of child nodes in group by calling bringToTop or sendToBack or by setting its z property (only within single group)

Link to comment
Share on other sites

Yes this seems reasonable but still I am not convinced. I will post additional code afterwards. 

Let's just say group1 was created first and group2 was created second.

If I add element1 to group2 first and then element2 to group1 element2 will appear on top of element1 as you can see in the link I posted above. As if the order which one is added first no matter in which group effects which is on top of what.

Link to comment
Share on other sites

I answered in your other post, but just in case...

Tom is correct. Last added is on top. Since it's the group's order that matters (not the order you add the sprites to the group), the group you added last will be on top.

From that other post, I think you're trying to add one sprite to two groups. In that case, the sprite would only be in the group you added it to last. That might explain the z-order problem you're seeing.

Link to comment
Share on other sites

There are many solutions to any problem.

An easier (lazy) solution I use is to use a Phaser Group for z-index sorting and Javascript arrays for any logical grouping I want to do. There's a workaround to getting phaser objects into multiple groups, someone posted a while ago, can't seem to find it :unsure:.

But with the Array solution, you're object can be in as many arrays as you want.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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