Cabin Crew 35 · Build phone controls and test swipes

Cabin Crew Express · Lesson 35 of 50 · Intermediate to advanced · About 38 min

A player's hand might cover part of the phone. Keep buttons easy to aim at and make each gesture count once.

Storage cupboards, water and folded supplies in the compact onboard galley.
What you’ll make Connect large touch controls, arrow keys and a vertical cabin swipe to the same move function.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/34 when the lesson builds on prior work, or copy the checkpoints/35 folder to try this lesson on its own. Read the file names in the steps before editing.

Touch down. Record a finger ID and start point. → Touch up. Subtract the start from the end. → Check direction. Accept a long vertical swipe. → Move once. Use the same rule as the large buttons.
Touch down → Touch up → Check direction → Move once
Godot checkpoint window for Build phone controls and test swipes
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson InputEventScreenTouch, InputEventKey, Rect2.has_point, guard clause, clampi.

  1. Open the editable checkpoint

    Start Godot 4.7.2, choose Import in Project Manager and select the project.godot file for this lesson. Open main.gd in FileSystem, then press F5. Stop the game with F8 before editing.

    Why it works This folder is its own project with a Control scene and an attached script. You can run it without files from any other lesson.

  2. Build two comfortable touch targets

    Click Toward row 6, then use Compact / wide layout and repeat. Compare gaps between the controls.

    move_down.position = Vector2(48, 338) if compact else Vector2(224, 280)

    Why it works Both arrangements use Button nodes with the same signals. Moving a Control does not change the turn it requests.

  3. Start the gesture in the right place

    Read the first conditional inside _input. On a touchscreen, start a gesture in the swipe zone, then outside it.

    if event.pressed and contact == -1 and Rect2(445, 172, 480, 190).has_point(event.position):

    Why it works A touch only claims the gesture if it begins in the chosen rectangle. The event index belongs to the finger that began it.

  4. Read the real movement of a finger

    In the release branch, follow event.position - origin before looking at swipe. Compare an upward release with a downward one.

    swipe(event.position - origin)

    Why it works Subtracting the start gives a change in pixels. A positive y goes down the screen.

  5. Reject a movement that was probably a tap

    Change SWIPE_MIN to 600 and try a normal swipe. Restore it and check that a vertical swipe works.

    if absf(delta.y) < SWIPE_MIN or absf(delta.y) <= absf(delta.x):

    Why it works A short or mostly sideways movement is unlikely to mean move along the aisle. The guard leaves the row as it was.

  6. Test keys and boundaries

    Press down and up in both layouts. Keep pressing down until the status reaches row 6, then test once more.

    at_row = clampi(at_row + direction, 0, 6)

    Why it works Every accepted input calls move. clampi returns just one location between the galley and the sixth row.

Watch the values change

  1. Down button direction is 1.
    What you see. Ari advances one row.
  2. Swipe sideways horizontal distance meets or exceeds vertical distance.
    What you see. The row stays the same.
  3. Switch layout Only compact and the button position change.
    What you see. Buttons are stacked and still send the same commands.

Predict before you run A swipe travels 50 pixels sideways and 35 down. Does it move Ari?

Reveal the answer

No. The vertical distance is shorter than the sideways distance, so the guard reports an ignored gesture.

Complete checkpoint

Buttons and up/down keys change one row from 0–6. Compact / wide moves the controls, and a vertical touchscreen swipe in the zone can move one row.

main.gd

extends Control

const SWIPE_MIN := 28.0
var at_row := 0
var origin := Vector2.ZERO
var contact := -1
var compact := false
var position_label: Label
var mode_label: Label
var move_up: Button
var move_down: Button

func _ready() -> void:
	heading("Make the same controls work on a phone", "TOUCH")
	mode_label = label("Wide preview • press Compact to compare", Vector2(48, 127))
	position_label = label("Galley 0 • use a button, arrow key or swipe", Vector2(48, 190))
	move_up = button("↑ Toward galley", Vector2(48, 280), move.bind(-1), 162)
	move_down = button("↓ Toward row 6", Vector2(224, 280), move.bind(1), 162)
	button("Compact / wide layout", Vector2(655, 394), switch_layout, 220)

func _input(event: InputEvent) -> void:
	if event is InputEventScreenTouch:
		if event.pressed and contact == -1 and Rect2(445, 172, 480, 190).has_point(event.position):
			contact = event.index
			origin = event.position
		elif not event.pressed and event.index == contact:
			contact = -1
			swipe(event.position - origin)
	if event is InputEventKey and event.pressed and not event.echo:
		if event.keycode == KEY_UP or event.keycode == KEY_DOWN:
			move(1 if event.keycode == KEY_DOWN else -1)
			get_viewport().set_input_as_handled()

func swipe(delta: Vector2) -> void:
	if absf(delta.y) < SWIPE_MIN or absf(delta.y) <= absf(delta.x):
		position_label.text = "Row %d • gesture was too short or sideways" % at_row
		return
	move(1 if delta.y > 0.0 else -1)

func move(direction: int) -> void:
	at_row = clampi(at_row + direction, 0, 6)
	position_label.text = "Galley 0 • one input moves one row" if at_row == 0 else "Row %d • one input moves one row" % at_row

func switch_layout() -> void:
	compact = not compact
	move_down.position = Vector2(48, 338) if compact else Vector2(224, 280)
	mode_label.text = "Compact preview • large stacked controls" if compact else "Wide preview • controls side by side"

func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))
	draw_rect(Rect2(445, 172, 480, 190), Color("#2a5062"))
	for index in range(1, 6):
		var y := 172.0 + 31 * index
		draw_line(Vector2(461, y), Vector2(909, y), Color("#5e8594"), 1.0)
	draw_string(ThemeDB.fallback_font, Vector2(599, 273), "Swipe zone on touchscreens", HORIZONTAL_ALIGNMENT_LEFT, -1, 17, Color.WHITE)

func heading(caption: String, topic: String) -> void:
	label("CABIN CREW EXPRESS     /     " + topic, Vector2(48, 41), 15, Color("#ffca7b"))
	label(caption, Vector2(48, 73), 27)

func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:
	var item := Label.new()
	item.text = text
	item.position = at
	item.add_theme_font_size_override("font_size", pixels)
	item.add_theme_color_override("font_color", ink)
	add_child(item)
	return item

func button(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:
	var item := Button.new()
	item.text = text
	item.position = at
	item.custom_minimum_size = Vector2(width, 47)
	item.pressed.connect(action)
	add_child(item)
	return item

GDScript line by line

Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.

Read main.gd line by line
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const SWIPE_MIN := 28.0Set the smallest useful gesture so incidental movement does not steer Ari.
  3. var at_row := 0Creates a variable named at_row. The expression uses the whole number 0. Godot infers the type from the first value.
  4. var origin := Vector2.ZEROCreates a variable named origin. The expression reads Vector2.ZERO. Godot infers the type from the first value.
  5. var contact := -1Use a negative ID when no finger currently owns the swipe gesture.
  6. var compact := falseCreates a variable named compact. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  7. var position_label: LabelDeclares position_label for later use. A colon followed by a type says which kind of value belongs here.
  8. var mode_label: LabelDeclares mode_label for later use. A colon followed by a type says which kind of value belongs here.
  9. var move_up: ButtonDeclares move_up for later use. A colon followed by a type says which kind of value belongs here.
  10. var move_down: ButtonDeclares move_down for later use. A colon followed by a type says which kind of value belongs here.
  11. func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls.
  12. heading("Make the same controls work on a phone", "TOUCH")Calls heading with the values in its parentheses.
  13. mode_label = label("Wide preview • press Compact to compare", Vector2(48, 127))Stores the new value in mode_label. The expression calls label with the values in its parentheses.
  14. position_label = label("Galley 0 • use a button, arrow key or swipe", Vector2(48, 190))Stores the new value in position_label. The expression calls label with the values in its parentheses.
  15. move_up = button("↑ Toward galley", Vector2(48, 280), move.bind(-1), 162)Stores the new value in move_up. The expression calls button with the values in its parentheses.
  16. move_down = button("↓ Toward row 6", Vector2(224, 280), move.bind(1), 162)Stores the new value in move_down. The expression calls button with the values in its parentheses.
  17. button("Compact / wide layout", Vector2(655, 394), switch_layout, 220)Calls button with the values in its parentheses.
  18. func _input(event: InputEvent) -> void:Godot calls this when input arrives. The event tells the game which key, pointer or touch action happened.
  19. if event is InputEventScreenTouch:Give raw touchscreen contacts their own block without making buttons depend on touch.
  20. if event.pressed and contact == -1 and Rect2(445, 172, 480, 190).has_point(event.position):Only a finger starting in the designated swipe zone can begin an aisle gesture.
  21. contact = event.indexRemember which exact finger began the gesture.
  22. origin = event.positionSave the pixel coordinate where the gesture began for comparison on release.
  23. elif not event.pressed and event.index == contact:Only finish for the release event of the same tracked contact.
  24. contact = -1Stores the new value in contact. The expression reads -1.
  25. swipe(event.position - origin)Subtract positions to measure direction and distance without using absolute screen coordinates.
  26. if event is InputEventKey and event.pressed and not event.echo:Ignore the repeated key events produced while a key is held.
  27. if event.keycode == KEY_UP or event.keycode == KEY_DOWN:Checks event.keycode == KEY_UP or event.keycode == KEY_DOWN. The indented instructions below run only if this test passes.
  28. move(1 if event.keycode == KEY_DOWN else -1)Calls move with the values in its parentheses.
  29. get_viewport().set_input_as_handled()Keep a handled arrow key from also acting as focus movement further down the input path.
  30. func swipe(delta: Vector2) -> void:Defines swipe, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  31. if absf(delta.y) < SWIPE_MIN or absf(delta.y) <= absf(delta.x):Reject a swipe if its vertical part is too short or not larger than its sideways part.
  32. position_label.text = "Row %d • gesture was too short or sideways" % at_rowUpdates position_label.text, which controls the words a player sees. The expression uses the text inside the quotes.
  33. returnFinishes the function right here. Any later instructions in this function are skipped.
  34. move(1 if delta.y > 0.0 else -1)Positive pixel y points down, so a downward gesture advances the row index.
  35. func move(direction: int) -> void:Defines move, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  36. at_row = clampi(at_row + direction, 0, 6)Send touch, keyboard and GUI presses through the same row boundary check.
  37. position_label.text = "Galley 0 • one input moves one row" if at_row == 0 else "Row %d • one input moves one row" % at_rowUpdates position_label.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  38. func switch_layout() -> void:Defines switch layout, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  39. compact = not compactStores the new value in compact. The expression reads not compact.
  40. move_down.position = Vector2(48, 338) if compact else Vector2(224, 280)Put the second button below or beside the first, while keeping its original action and accessible text.
  41. mode_label.text = "Compact preview • large stacked controls" if compact else "Wide preview • controls side by side"Updates mode_label.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  42. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  43. draw_rect(Rect2(Vector2.ZERO, size), Color("#102336"))Paints a rectangle at the given position and size.
  44. draw_rect(Rect2(445, 172, 480, 190), Color("#2a5062"))Paints a rectangle at the given position and size.
  45. for index in range(1, 6):Repeats the indented instructions for each value in range(1, 6).
  46. var y := 172.0 + 31 * indexCreates a variable named y. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  47. draw_line(Vector2(461, y), Vector2(909, y), Color("#5e8594"), 1.0)Paints a line between two points with the chosen colour and width.
  48. draw_string(ThemeDB.fallback_font, Vector2(599, 273), "Swipe zone on touchscreens", HORIZONTAL_ALIGNMENT_LEFT, -1, 17, Color.WHITE)Paints a piece of text with a font at a position measured from the node's origin.
  49. func heading(caption: String, topic: String) -> void:Defines heading, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  50. label("CABIN CREW EXPRESS / " + topic, Vector2(48, 41), 15, Color("#ffca7b"))Calls label with the values in its parentheses.
  51. label(caption, Vector2(48, 73), 27)Calls label with the values in its parentheses.
  52. func label(text: String, at: Vector2, pixels: int = 20, ink: Color = Color("#f7f4e9")) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  53. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  54. item.text = textUpdates item.text, which controls the words a player sees. The expression reads text.
  55. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  56. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  57. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  58. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  59. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  60. func button(text: String, at: Vector2, action: Callable, width: float = 155.0) -> Button:Defines button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  61. var item := Button.new()Creates a variable named item. The expression creates a new Button object. Godot infers the type from the first value.
  62. item.text = textUpdates item.text, which controls the words a player sees. The expression reads text.
  63. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  64. item.custom_minimum_size = Vector2(width, 47)Updates item.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  65. item.pressed.connect(action)Links a signal to the function that should run when the event happens.
  66. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  67. return itemFinishes the function and hands its result back to the caller. The expression reads item.

Find and fix a bug

A desktop mouse may not send InputEventScreenTouch. On desktop test buttons and keys. Check swipes on a touch-enabled device or with a configured touch emulator.

Try a change

Ages 10–12 can try wider button gaps. Ages 13–16 can read the Control size on resize and automatically choose the stacked layout.