How to set a button in the 2d world

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

I have been using the Godot engine for a short time and now I want to create a small 2D strategy game.
In the game there are cities, in my project these are a scene with a Node2D as root. To click on a city I have added a button as child node. My problem is that I just can’t figure out how to nail a button to the position of the Node2D. The button is always too big and does not behave correctly.

Can you provide screenshots?

axilirate | 2020-04-06 09:34

:bust_in_silhouette: Reply From: njamster

How to nail a button to the position of the Node2D

If the button is a direct child of the Node2D, it already is! The position-property is always relative to the parent-node, it it’s set to (0, 0) the top-left corner will be the position of the parent.

The button is always too big

You can set the rect_size-property to precisely the value you want, let’s say (200, 20). If you want the button to be centered on the position of it’s parent-node, set the rect_position accordingly, i.e. in this case to (-100, 10).

and does not behave correctly

Which behavior do you expect? And what’s happening instead?

Thanks for the nice answer, I have found a solution.

MaxEl777 | 2020-04-06 12:46

a scene where a sprite has a size of 1

I’m assuming you’re talking about the Sprite’s scale here, not it’s size?

So why is the sprite not 16 times smaller but almost as big as the tile.

Why should it? If you have a Sprite and assign to it an Image of size 16x16 pixels, it will be 16x16 pixels wide. That doesn’t change if you add it as a child of another scene.

njamster | 2020-04-06 13:03