Group.setDepth and assigning a sprite to it, does not sort depth

Recommended Posts

Hey, when I assign setDepth(number) to Groups and Tilemaps, it seems not to work.
Only after setting setDepth(number) on an already added sprite, it works.

Any Ideas on it? Complete code below.


// Tilemaps already created

// # Configure z indexes
layer.background.setDepth(0); //dynamic tilemap layer
layer.midground.setDepth(1); //dynamic tilemap layer
layer.objects.setDepth(2); // group
layer.enemies.setDepth(3); // group
layer.player.setDepth(4);  // group
layer.ground.setDepth(5);   //dynamic tilemap layer
layer.effects.setDepth(6); // group
layer.water.setDepth(7);  //dynamic tilemap layer

//later on
const SPRITE = this.add.sprite(0,0,'player')

//result: SPRITE is not in front of layer.background ..

// BUT when I also write:

const SPRITE = this.add.sprite(0,0,'player')

// Now the sprite is before layer.background... ?

Share this post

Link to post
Share on other sites

Groups in v3 don't work the same as in v2, they're not containers. If you call Group.setDepth it will (as the docs say) immediately set the depth value of each child. If it has no children, or a child is added after you call this (like in the code above), it won't make any difference.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.