![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Fupicat |
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