• 2D
  • InputEventScreenTouch and InputEventScreenDrag problem.

Hi, i'm currently trying to built a simple game for android with a player moving to a location pressed using InputEventScreenTouch, and jumping when i swipe up using InputEventScreenDrag.

i have set up a simple node2d scene to run and test the input, this is the code: func _input(event):

 if event is InputEventScreenTouch:

	 if event.is_pressed():

		 print("touched")

 if event InputEventScreenDrag :

	print("dragged")

when i touch the screen the InputEventScreenTouch fires up and prints touch, but the problem is when i drag it looks like the InputEventScreenTouch prints touched aswell and right after InputEventScreenDrag works just fine, but that initial touch always happends no matter which way i go about it, any advice? thanks!

Welcome to the forums @AviY!

The issue is that initially you are getting a touch and a drag on the same frame, correct? If so, if the order is always "touched" then "dragged", what you could do is make a boolean to tell whether the screen has already been touched, and then skip the drag event if the boolean is true. Something like this:

var skip_drag_due_to_input_touch = false

func _process():
	skip_drag_due_to_input_touch = false

func _input(event):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			print ("touched")
			skip_drag_due_to_input_touch = true
	if event is InputEventScreenDrag:
		if skip_drag_due_to_input_touch == false:
			print ("dragged")

What about checking for a drag event first, and ignoring a touch event in that case? Is that the wrong approach?

if event is InputEventScreenDrag:
	# process drag
elif event is InputEventScreenTouch:
	# process touch

@TwistedTwigleg said: Welcome to the forums @AviY!

The issue is that initially you are getting a touch and a drag on the same frame, correct? If so, if the order is always "touched" then "dragged", what you could do is make a boolean to tell whether the screen has already been touched, and then skip the drag event if the boolean is true. Something like this:

var skip_drag_due_to_input_touch = false

func _process():
	skip_drag_due_to_input_touch = false

func _input(event):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			print ("touched")
			skip_drag_due_to_input_touch = true
	if event is InputEventScreenDrag:
		if skip_drag_due_to_input_touch == false:
			print ("dragged")

i'll check it out, i solved the problem in a different way, this might be a better solution, i basically need the single tap for movement and the drag for jump, so i just checked if !event.is_pressed(): then the tap option will only happen when i release my finger. thanks! i'll update here

@DaveTheCoder said: What about checking for a drag event first, and ignoring a touch event in that case? Is that the wrong approach?

if event is InputEventScreenDrag:
	# process drag
elif event is InputEventScreenTouch:
	# process touch

i have tried it, but it doesn't work because the drag function first works as a touch function so no matter if i "ignore" the touch it does it first anyway