Mouse drag and release on RigidBody2d

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


I am attempting to write some elegant code to allow a player to click on a sprite and drag a line out of the boundary of the sprite and release the mouse. The sprite is a child of a rigidbody2d node (and ideally this would be in the rigidbody2d script at the top of that particular scene). I have 2 questions.

I am using this “harness” to try and get the structure of the code correct.

func _input_event(viewport, event, shape_idx): #handles a mouse down within the rb2d
if event is InputEventMouseButton:
	if event.is_pressed():
		print('mouse pressed')
		_dragging = true

		print('mouse released')
		_dragging = false

elif event is InputEventMouseMotion:
	if _dragging:
		print('mouse motion')

elif event is InputEventKey:
	if event.get_scancode() == KEY_SPACE && event.is_pressed() == false:
		print('space pressed')

First question. I am seeing dual print outs for ‘mouse pressed’ and ‘mouse released’. Why is that?

Second question, when I release the mouse outside of the rigidbody2d, I do not get the mouse released. Is the input event only available inside the confines of the collisionshape associated with that node? Do I have to handle the mouse pressed code in the input event signal handler and the mouse release code in the generic _input handler?