Question on touch for a pong game (2 x touches happening (or more))

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’m assuming something like this:

func _input(event):
    if event.type == InputEvent.SCREEN_TOUCH:
        # Do touchy touchy

I want two player though, so I’m guessing it would be something like:

func _input(event):
    if event.type == InputEvent.SCREEN_TOUCH:
        if touch is on L side of screen do left controller
        elif touch is on R side of screen  do right controller.

Before I commit to this I thought I’d ask and see what people think of this approach or if they can see any flaws / better solutions?

Thanks a tonne…

I think I would do that too.

volzhs | 2017-02-17 15:33

:bust_in_silhouette: Reply From: eons

If the game don’t need drag, you can use big invisible TouchScreenButtons, then just control the input or events actions like any action, these nodes work fine with multitouch.

That is interesting. I’ve not researched touch buttons. I’ll take a peek and see how they behave

Robster | 2017-02-19 00:21

:bust_in_silhouette: Reply From: Robster

For future reference to help anyone else. This is how I’m doing it (so far working well):

I have a structure like so:

  • gameLevel
    • Area2DL (script here)
      • CollisionShape2D (rectangle shape)
    • Area2DR (script here)
      • CollisionShape2D (rectangle shape)

On the Area2DL and Area2DR there is a signal called input_event. I connect that signal to a custom function as shown in this complete script:

extends Area2D

var touchPos	=	Vector2(0,0)	#this will change when we touch the Area2D

func _ready():
	# Called every time the node is added to the scene.

func _fixed_process(delta):
	get_node("/root/gameLevel/batIndicatorL/KinematicBody2D").touchControl(delta, touchPos)

func _on_Area2DL_input_event( viewport, event, shape_idx ):
	touchPos = event.pos  #this is a Vector2 with local position of the touch

The get_node("/root/gameLevel/batIndicatorL/KinematicBody2D").touchControl(delta, touchPos) is essentially a function that drives the movement of my bat. It takes the delta and the touchPos and feeds it to the function like so:

func touchControl(delta, touchPos):
	if PlayerNumber == 1:
		move_to(Vector2(30, touchPos.y))
	elif PlayerNumber == 2:
		move_to(Vector2(510, touchPos.y))

In this way, the Area2D passes the location of the touch to the bat to move to that position in the Y only. Nice!
It’s not perfect I’m sure but it’s working so far. Now to test on a few devices.

Many thanks to volzhs and eons for the suggestions.