Create touch controls for the tutorial my first 2d game

Godot Version

4.2.1

Question

I just finished the tutorial for creating the first 2D game that is in the official gotod 4 guide. Now I am trying to create a canvas layer with touch controls to test it on Android, I have created the scene with the basic buttons for the necessary movement in that simple game but I don’t know how to pair this with the game. Do I have to create an additional script for it to work or connect to a function? I do not understand this part.

This is the script that I have configured for player as shown in the godot4 tutorial:
everything works perfect, what I need is to add functional touch controls.

extends Area2D

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.

func _ready():
screen_size = get_viewport_rect().size
hide()

func _process(delta):
var velocity = Vector2.ZERO # The player’s movement vector.
if Input.is_action_pressed(“move_right”):
velocity.x += 1
if Input.is_action_pressed(“move_left”):
velocity.x -= 1
if Input.is_action_pressed(“move_down”):
velocity.y += 1
if Input.is_action_pressed(“move_up”):
velocity.y -= 1

if velocity.length() > 0:
	velocity = velocity.normalized() * speed
	$AnimatedSprite2D.play()
else:
	$AnimatedSprite2D.stop()
position+=velocity*delta
position=position.clamp(Vector2.ZERO, screen_size)
if velocity.x != 0:
	$AnimatedSprite2D.animation = "walk"
	$AnimatedSprite2D.flip_v = false
# See the note below about boolean assignment.
	$AnimatedSprite2D.flip_h = velocity.x < 0
elif velocity.y != 0:
	$AnimatedSprite2D.animation = "up"
	$AnimatedSprite2D.flip_v = velocity.y > 0

signal hit

func _on_body_entered(body):
hide() # Player disappears after being hit.
hit.emit()
# Must be deferred as we can’t change physics properties on a physics callback.
$CollisionShape2D.set_deferred(“disabled”, true)

func start(new_position):
position = new_position
show()
$CollisionShape2D.disabled = false

1 Like

Hello Mate,

push a TouchScreenButton and take a look at the signals. It should be look like :

func _on_arriba_pressed(): # I don't know spanish xD
	print("up") # arriba? 
        velocity.y = -5

func _on_arriba_released():
        velocity.y = 0

This is just an example. Play a little bit with the signals. :smiley:

1 Like

Hello friend, sorry I don’t understand your explanation. I think that you have not understand me. I have a scene where I have the four buttons of up, down, left, right, and I have the scenes apart from player, hud, mobs, main and player. I want the canvas layer scene that I created with the touchscreenbuttons to appear on the screen when I press play so that the game is controlled with touch controls and not from the PC keyboard. I don’t know how to attach touch controls to the game

1 Like

You need to use the signals from the buttons.

1 Like

ok, helpme

1 Like

If you’re using TouchScreenButton nodes, just set the “action” they fire when pressed in the inspector and they will work as any other kind of controller.
There’s no need for signals or anything like that.

3 Likes

ok, this action? dont work

please, look

1 Like

I’m confused on this part of getting the touch to work. It would be better if I could swipe the character with one finger but I can’t figure out how to do it either.

1 Like

You need to add an action to the touchscreen button. Otherwise nothing happens, script is nice btw

1 Like

Your script says move_up, you need to use the same action as in your code.

Gestures are harder to make and use, you may find addons with some functionality.

2 Likes

I read the entire guide, I have learned a lot but I can’t find a section that explains how to make the touch buttons work or swipe with one finger. I built the apk, the game works perfectly when I install it on the phone but I can’t move the character because it doesn’t have a way to move it by touch. It’s what I want to learn and I don’t know how. If you can help me it would be great

1 Like

finally someone who understood my question!! I love you xD brother, thank you. And why is it move_up and not ui_up as in the standard engine configuration?

1 Like

Do you know any way to configure gestures? Do you have any video out there or a guide?

1 Like

I would also like these buttons to appear on the screen only after having pressed [GO] could you help me with this?

1 Like

you’ll have to set up the game states and/or maybe signals to help deciding when hide and show things, try with some simple setups until you get the effect you want.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.