![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Droggelbecher |
Hello,
i am looking for an efficient way to make some 2D sprites clickable / react to mouse inputs. I found this code snippet online:
===================
extends Sprite
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed and not event.is_echo() and event.button_index == BUTTON_LEFT:
var pos = position + offset - ( (texture.get_size() / 2.0) if centered else Vector2() )
if Rect2(pos, texture.get_size()).has_point(event.position):
print(‘test’)
get_tree().set_input_as_handled()
===================
I tested this with an centered 64x64 rectangle as a sprite. The code itself worked, but unfortunately the Rect2 object created in the script is a bit off on the y-axis and does not fit the sprite. I tried to fix this by manually adjusting the position of the Rect2 object, but the adjustment would have to be made for each other sprite individually.
Do you know a solution to this problem or a better way to select sprites per mouse click?