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?

in Engine by (15 points)

1 Answer

+2 votes
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.

by (27,902 points)
selected by

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.