How to click on a sprite and move it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By breepell
:warning: Old Version Published before Godot 3 was released.

Hi,

I’m in a bit of a predicament. I’m trying to create a map puzzle so basically moving the states to their correct location. Unfortunately I can’t even click on the state never mind moving it. I know there is some similar questions out there but none of them have worked.

I’ve tried the sprite as an a child and the parent of an area2d with a collisionShape2d. didn’t work. I tried connecting an input to the area2d and collisionShape but I got a target error so I added a a script to the collision still didn’t work. The only thing that seemed to have any kind of effect was adding the sprite to a control and using an input which automatically coded it for me. But it seemed I couldn’t even click on it and I couldn’t even replicate the results. Sometimes it would completely crash the preview.

I don’t if this has any effect or if it’s helpful but I’m using 2.1.4 and a Mac.

Is there something I’m missing? what would be the right way going about this?

Would you mind providing a minimal sample project including the code you have already written?

rolfpancake | 2017-12-16 10:34

The code I’m using finally worked but the problem is that when the states overlap you can’t grab each one. I attached the code and and the file. I thought about using a rigid body but that could cause issues considering it’s supposed to be online. Maybe it’s not even the right code for it to work properly?

extends Sprite

var dragging = false

var status = “none”
var tsize=Vector2()
var offset=Vector2()
var mpos=Vector2()

func _ready():
tsize=get_texture().get_size()
set_process_input(true)
set_process(true)

func _process(delta):
if status == “clicked”:
set_global_pos(mpos + offset)

func _input(ev):
if ev.type == InputEvent.MOUSE_BUTTON:
if ev.button_index == BUTTON_LEFT:
if ev.is_pressed() and _is_clicked(ev.global_pos):
status = “clicked”
mpos = ev.global_pos
offset = get_global_pos() - mpos
print(“clicked”)
else:
status = “released”
print(“released”)
elif ev.type == InputEvent.MOUSE_MOTION:
if status == “clicked”:
print(“move”)
mpos = get_viewport().get_mouse_pos()

func _is_clicked(pos):
var sprite_rect
var gpos = get_global_pos()
if is_centered():
sprite_rect = Rect2(gpos.x - tsize.x/2, gpos.y - tsize.y/2, tsize.x, tsize.y)
else:
sprite_rect = Rect2(gpos.x, gpos.y, tsize.x, tsize.y)

if sprite_rect.has_point(pos):
   return true

the game

breepell | 2017-12-19 13:22

:bust_in_silhouette: Reply From: Zylann

Those existing answers may be useful:

https://forum.godotengine.org/11765/what-is-under-my-finger-touch-screen?show=11771#a11771

https://forum.godotengine.org/13504/how-can-i-get-the-mouse-event-in-a-object?show=13588#a13588

https://forum.godotengine.org/3719/how-to-get-a-node-by-mouse-click?show=3719#q3719

You may have a look at the pickable property in your collision object, which allows bodies or areas to receive input like GUI controls.

I got it work Yay, I think it had to do with some indentation issues! The only weird thing that happens is that it sometimes picks multiple objects at once.

breepell | 2017-12-18 12:39