Touch input
You'll learn to make the whole screen listen for a tap and change the game when it comes.
Watch & read
Start with the required ones. The others are there when you need them.
Explanation
You want the square to jump when the player taps, anywhere on the screen. Small buttons are hard to hit in a fast game, so the whole screen becomes the button.
Think of a doorbell. Pressing it does not open the door by itself. It only tells someone inside, and they decide what to do. A tap is the same: it calls your function, and your function changes the game.
Pressable turns anything into something you can tap. Wrap the whole screen in one, with flex: 1:
<Pressable style={styles.screen} onPressIn={() => setGame((g) => handleTap(g))}>{/* the world, the square, the obstacle */}</Pressable>
Pressable has two events you will meet. onPress runs when the finger lifts. onPressIn runs the
moment the finger touches. A game wants the jump right away, so we use onPressIn.
handleTap is a plain function, like step. It takes the game and returns the next one:
function handleTap(game) {if (game.playerY > 0) return game;return { ...game, playerY: JUMP_HEIGHT };}
On the ground (playerY is 0) a tap puts the square up at 150. Then the game loop pulls it down, 10
points every tick, until it is back on the ground. That is our first, simple jump. It goes up all at once,
which looks a bit rough. The next topic makes it smooth.
People often let the player jump in the air. Without the if, tapping fast sends the square up again
and again, and it never comes down. The game becomes too easy, and the player can fly.
When you read touch code in a game, find the Pressable, see which event it uses, and read the function
it calls. That function, not the Pressable, decides what the tap does.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1const JUMP_HEIGHT = 150;2const FALL_SPEED = 10;34function handleTap(game) {5 if (game.playerY > 0) return game;6 return { ...game, playerY: JUMP_HEIGHT };7}89function fall(game) {10 return { ...game, playerY: Math.max(0, game.playerY - FALL_SPEED) };11}1213let game = { obstacleX: 300, playerY: 0, score: 0 };14game = handleTap(game); // first tap15game = fall(game); // tick16game = fall(game); // tick17game = handleTap(game); // second tap, in the air18console.log(game.playerY);
handleTap jumps on every tap, even in the air, so the player can fly. Change it: when playerY is above 0, give back the game unchanged. On the ground, jump to JUMP_HEIGHT like now.1import { useEffect, useState } from "react";2import { Pressable, StyleSheet, Text, View } from "react-native";34const SPEED = 10;5const SCREEN_WIDTH = 400;6const OBSTACLE_WIDTH = 30;
Runs in the browser. Choose “My Device” in Snack to try it on your phone with Expo Go.
onPressIn to onPress, then hold your finger down. The jump only comes when you lift. Change it back.Make the jump higher by changing JUMP_HEIGHT to 200.Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3Why does our game use
onPressIninstead ofonPress? - 2 / 3How do we make a tap anywhere on the screen count?
- 3 / 3The square is in the air with
playerYat90. What doeshandleTapreturn?