0 votes

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?

in Engine by (12 points)

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

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().getsize()
set
processinput(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.buttonindex == BUTTONLEFT:
if ev.ispressed() and _isclicked(ev.globalpos):
status = "clicked"
mpos = ev.global
pos
offset = getglobalpos() - mpos
print("clicked")
else:
status = "released"
print("released")
elif ev.type == InputEvent.MOUSEMOTION:
if status == "clicked":
print("move")
mpos = get
viewport().getmousepos()

func isclicked(pos):
var spriterect
var gpos = get
globalpos()
if is
centered():
spriterect = 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

1 Answer

0 votes

Those existing answers may be useful:

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

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

https://godotengine.org/qa/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.

by (27,898 points)

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.

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.