Update Area2D location

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

Hey guys,

My question would be - how can I update the Area2D location after mouse click? I need to move my Area2D object to the other side of the screen after the mouse click and after the second click - return an object to the primary location on the screen. Thanks

EXAMPLE - Imgur: The magic of the Internet

:bust_in_silhouette: Reply From: njamster

Attach this script to your Area2D and change pos1 and pos2 to your liking:

extends Area2D

var pos1 = Vector2(100, 100)
var pos2 = Vector2(300, 300)

func _ready():
	global_position = pos1
    connect("input_event", self, "_on_Area2D_input_event")

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
		if global_position == pos1:
			global_position = pos2
		else:
			global_position = pos1