Click to bring 2D sprite to front while keeping the order same for all other sprites

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By calimbri

Hello. I want to click to bring a sprite to the front. Then when I click a different sprite, it should also come to the top. Just setting the z_layer to 999 on click and 0 to release doesn’t work because the top sprite can end up hiding behind another one (default sorting order). I’m wondering if there is a built in function in Godot or is there another method for this?

:bust_in_silhouette: Reply From: Zylann

Just setting the z_layer to 999 on click and 0 to release doesn’t work because the top sprite can end up hiding behind another one (default sorting order)

I don’t really understand what’s the problem here. Did you try to change z_as_relative? Node2D — Godot Engine (stable) documentation in English

If you don’t want to rely on Z-order, another way is to use tree order. Sprites are drawn in the order they come in the tree, so if you want to bring a sprite forward, move it at the bottom of its siblings:

- Parent
    - Sprite1 < -- The sprite you want to be drawn last
    - Sprite2
    - Sprite3

Using raise(): Node — Godot Engine (stable) documentation in English

- Parent
    - Sprite2
    - Sprite3
    - Sprite1 <-- move it here

This is also what popup GUI nodes use internally.

raise() was just what I was looking for. Thank you.

calimbri | 2020-05-12 23:11