+1 vote

I would like to drag and drop multiple sprites on touchscreen mobile.

I successfully did it for desktop (mouse).

I created an Area2d->CollisionShape for each sprite and used the callbacks onenter and InputEvent.MOUSEBUTTON event.

How can i implement this on Android (touchscreen) ?

in Engine by (13 points)

Im sorry i know this is old and I don't have a answer for you but can you please show how you got drag&drop to work with mouse? Like show the script.

No problem.
Here is my code:

extends Sprite

var pressed = false
var mouse_enter = false
var object_dragged = false
var pos = get_global_mouse_pos()

func _ready():
    set_fixed_process(true)

func _fixed_process(delta):
    pos = get_global_mouse_pos()
    if mouse_enter == true:
        if Input.is_mouse_button_pressed(BUTTON_LEFT):
            set_global_pos(pos)
            object_dragged = true
        else:
            object_dragged = false

func _on_Area2D_mouse_enter():
    mouse_enter = true
    print("enter")

func _on_Area2D_mouse_exit():
    if object_dragged == false:
        mouse_enter = false

1 Answer

0 votes

I'm using an input_event function:

func _on_Pistolo_input_event( viewport, event, shape_idx ):
    if event.type == InputEvent.SCREEN_TOUCH:
        if event.pressed:
            get_tree().get_root().get_node("Radiko").kaptitajxo = self
        else:
            get_tree().get_root().get_node("Radiko").kaptitajxo = null
    elif event.type == InputEvent.MOUSE_BUTTON:
        if event.button_mask == 0:
            get_tree().get_root().get_node("Radiko").kaptitajxo = self
        else:
            get_tree().get_root().get_node("Radiko").kaptitajxo = null

I keep current picked up instance in a variable like kaptitajxo at root node.
Finally you need something like this in order to updating position of picked up instance to mouse position:

if get_tree().get_root().get_node("Radiko").kaptitajxo != null:
        get_tree().get_root().get_node("Radiko").kaptitajxo.set_global_pos(get_global_mouse_pos())
by (14 points)
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.