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 _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
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
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.
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.
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
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?
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.