How can I use multiple draggable objects in a touchscreen?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jasshtag

Hi, guys, I’m new using this engine, and currently I’m trying to make a simple jigsaw game, so for the pieces of the jigsaw, I created a scene meant to be draggable, and works fine in my computer, but when I exported to my phone, I’ve face some several issues.

I implemented the mouse_entered signal to know wich piece can I drag, otherwise when various pieces are overlapping the mouse take em all, but this on a touchscreen isn’t working, cause there’s no way to execute the mouse_entered signal in that way, so when I touch a piece the focus remains in that one, so if I touch a different one the position of the first one keeps changing…

I’m sorry for my broken english, I’m really trying to explain my problem.

This is my code for the piece node:

extends Node2D

var selected : bool = false
var selectable: bool = true
var mouse_over: bool = false

onready var sprite = $face
onready var area = $Area
onready var col = $Area/CollisionShape2D
onready var ani = $AnimationPlayer

var sprtX
var sprtY
var row : int
var column : int 

var coord
signal _selected(Piece)
signal dropped
signal test

func _ready():
	sprtX = sprite.texture.get_size().x
	sprtY = sprite.texture.get_size().y
	col.shape.extents = Vector2(sprtX*.35,sprtY*.35)
	area.connect("mouse_entered",self,"_mouse_over", [true])
	area.connect("mouse_exited",self,"_mouse_over", [false])

func setCoordenates(r, c) -> void:
	row = r
	column = c

func getCoordenates() -> Vector2: return Vector2(row,column)

func getTextureSize() -> Vector2:
	return Vector2(sprtX,sprtY)

func _unhandled_input(event):
	if event is InputEventMouseButton and mouse_over:
		if event.button_index == BUTTON_LEFT and event.pressed:
			selected = true
			emit_signal("_selected", self)
			if event.button_index == BUTTON_LEFT and not event.pressed:
				selected = false
				var shortest_dist = 45
				var distance = global_position.distance_to(coord.global_position)
				if distance < shortest_dist:
					if getCoordenates() == coord.getCoords():
						if selectable:"selected")
							selectable = false
							global_position = coord.global_position

func _physics_process(delta):

func followMouse(delta):
	if selected and selectable:
		global_position = get_global_mouse_position()
func _mouse_over(over):
	self.mouse_over = over
:bust_in_silhouette: Reply From: shreyk27

Try this,

maybe this will help.

1 Like