Getting Depth from .bringToTop()

I am using self.children.bringToTop(gameObject) when someone clicks on a sprite. This is working fine and it visually moves the sprite to the top, over the other sprites in the scene. I want to then read that depth data to save it.

I do console.log(gameObject.depth); in the same function to be able to read it. It is always outputting 0.

Does bringToTop() also modify the .depth property, or does it work differently?

children.bringToTop() modifies the display list directly, and not the game object.

You can use children.getIndex(gameObject) to find a game object’s position or children.length to count them.

1 Like

Thanks.

Been having a hell of a time with this.

So all my interactive sprites are in a container, so I can’t use the depth functions.

During normal operation of the game I can use bringToTop() without a problem. My issue is that I send the z order of these spites to the server and they need to be reloaded back in the same depth order if the player reloads the game.

To get around the fact that I can’t use .depth, I have manually iterated a property called .zindex when a sprite it brought to the top. These values are then saved in the DB and retrieved without problem.

The issue I am having is now how to apply that depth value back.

I have tried sorting the array of data before looping through it and creating the sprites, to ensure that they are created in the same order that they were saved in. The sorting works as expected, but then the loading isn’t consistent.

I have tried using the built in Container.Sort(), which yields similar inconsistent results.

I have tried using the container.moveTo() using my zindex as the ‘index’ however I get an error that the index is out of bounds.

What am I doing wrong and what should I try next?