How to automatically set draw order of sprites?

How to automatically set draw order based on child index and y-axis coordinate when there are multiple sprites?
If i want make a 2d game, Do I have to manually set the draw order for each sprite?

Hi @fftidus,

Check this post: How to change the render order of sprite?

I know how to adjust the rendering order of sprites. What I want to ask is, if there are a large number of sprites, like in a tiled map, their sorting rules will be related to the group they are in and their own Y coordinate. Also, if a character moves, it will affect the rendering order of the sprites. Do I need to manually set the draw order for all of them or modify their Z-axis?

Setting the rendering order work only if you set the layer sort order to Manual. That’s optional, not required.

Normally the sprites are rendered from back to front, so yes, you can control them by adjusting their Z axis position.

After setting the sorting to “back to front”, modifying the sprite’s layer in the parent does change its rendering order, but only during runtime. It still appears incorrect in the editor.


run

let gObject = this.entity.findByName("g");
    let child0 = gObject.children[0];
    let child1 = gObject.children[1];
    gObject.removeChild(child0);
    gObject.removeChild(child1);
    gObject.insertChild(child1, 0);
    gObject.insertChild(child0, 1);

And i swapped the Sprite’s layer order through code, but it doesn’t seem to be effective.

Yes your code will not execute in editor.

About your previous question, why it doesn’t show in editor, I’m not really sure. But could you try and change your active camera in editor from Perspective to the your main game camera?

image

scene
Changing the editor’s camera did not take effect.

Can I understand that, in order to adjust the layer of a sprite through code, I can only modify the z-coordinate? Or do I have to manually manage the layers and use the draw order property?

You can do either of the two:

  • Adjust the Z position
  • Or set the layer order to manual and use the draw order property per sprite.
1 Like

Okay, thank you for your response.

1 Like