0 votes

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?

asked May 12 in Engine by calimbri (15 points)

1 Answer

+1 vote
Best answer

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? https://docs.godotengine.org/en/stable/classes/class_node2d.html#class-node2d-property-z-as-relative

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(): https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-raise

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

This is also what popup GUI nodes use internally.

answered May 12 by Zylann (26,786 points)
selected May 13 by calimbri

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.