0 votes

I'm creating a drag-and-drop script for 2d sprites. This is the code I'm using to detect left mouse button presses:

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:

The code above detects if the mouse button was either pressed OR released, which is really annoying. I want to add a timer that the player has to hold the button pressed before the sprite actually begins dragging, but I haven't found a solution. Having a way to detect if the button has been released would be way easier.

Here's my entire code:

extends Sprite

var mouse_on = false #Mouse over sprite
var dragging = false #Able to drag

func _process(delta):
    print(str($"Hold Timer".time_left))
    if dragging: #If the sprite is allowed to drag
        position = get_global_mouse_position() #Set the sprite's position to the mouses position

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: #This script detects if the left mouse button was either pressed or released
    if !dragging and mouse_on: #If dragging isn't allowed and the mouse is over the sprite
        dragging = true #Allow dragging
    elif dragging: #Else if the sprite is already dragging
        dragging = false #Stop dragging

func _on_Area2D_mouse_entered():
    mouse_on = true


func _on_Area2D_mouse_exited():
    mouse_on = false
in Engine by (85 points)

1 Answer

+3 votes
Best answer

Ok, I'm dumb. All I needed to do was add a left mouse button action to the input map and use isactionreleased().

Idk what I was thinking.

by (85 points)

Thank you for your dumbness, I was having the same problem

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.