Move to touch location / mouse click location within a bounded area?

: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 having a go at pong and want it to work on mouse, keyboard and touchscreen. So, I’m starting there, on the bats/paddles.
Touch input

Considering the left bat only at the moment. The idea is I have a space to the left of the bat where I can touch with my fat fingers. The bat then moves at X speed to that location. If I slide my finger down then the bat tries to continue to wherever I’m moving my finger to at X speed. This way I can use hand input.

Mouse Input

Basically the same idea. You click the mouse into the area to the left of the bat, it acts just like the touch system.

Keyboard input

Basic A/Z keys. Bat moves at X speed in appropriate direction. Not much to see here.

The Question

The question is, how would you implement the touch / move to? I’m considering something like an Area2d. Finding the Y location of the touch and then tweening the bat to that location.

I want this code to be as modular as possible. The last project got a bit spaghetti code. Hence I’m asking before just diving in.

Any advice greatly appreciated.

:bust_in_silhouette: Reply From: Zylann

I went doing a bit of testing, and I would structure the scene this way:

- LeftBatArea (Area2D)
    - CollisionShape (rectangle)
- LeftBat (KinematicBody2D + bat.gd)
    - Sprite
    - KeyboardInput (Node + bat_keyboard.gd)
    - MouseInput (Node + bat_mouse.gd)
    - TouchInput (Node + bat_touch.gd)

The bat.gd would only expose a command variable that contains -1, 1 or 0 and moves according to that parameter.

Then, every input node would reference the parent, and set the command for each possible input.
Mouse and touch might want to handle the input_event signal from LeftBatArea so they know when an event happened over the control area of the bat.

One thing to note, you should store the touch index so you know which finger controls the bat, in case your game can be played with multiple users on a touchscreen (doing that with the mouse is impossible because there can be only one mouse).

Another thing is, Godot emulates mouse event on touch devices, so you should test if the mouse input node enters in conflict with the touch input node or not.

Thanks for your help, I’m back from a break and getting back into it.

The bat.gd would only expose a command variable that contains -1, 1 or
0 and moves according to that parameter.

May I ask how the bat.gd would expose this variable? I’m really not the worlds best programmer as you can see. Learning all the time. :slight_smile:

Then, every input node would reference the parent, and set the command
for each possible input.

Would that be with a get_parent.variable = stuffhere type command?

RE the rest I’ll chip away but first things first. :slight_smile: Thanks again!

Robster | 2017-02-03 07:21