Workshop › OutRun-style Racer › Step 18 of 44
A pedal for phones
What we are doing
A phone has no arrow keys. A finger held anywhere on the game will be the gas pedal: touching presses ArrowUp in
keys, lifting the finger lets go.
```js
// Touch: hold anywhere to accelerate.
canvas.addEventListener('pointerdown', () => {
keys.ArrowUp = true
})
function stopTouch() {
keys.ArrowUp = false
}
canvas.addEventListener('pointerup', stopTouch)
canvas.addEventListener('pointercancel', stopTouch)
```
What it means
pointerdown happens when a finger (or the mouse) touches the canvas, pointerup when it lifts, and pointercancel
when the touch is interrupted.
- The same function,
stopTouch, handles both endings.
Your turn
Under the keyup listener (above the empty line before update), write the touch code.
Skills: Keyboard and mouse input
← Speedometer Move the camera sideways →
All steps
- Sky and ground
- Draw again and again
- Perspective
- One piece of road
- A road made of segments
- Where we are on the track
- The road ahead
- Stripes
- Paint from far to near
- Kerbs and a center line
- Striped grass
- Moving is one number
- Which keys are down?
- The gas pedal
- Brake, coast and a top speed
- Your car
- Speedometer
- A pedal for phones
- Move the camera sideways
- Steering
- No turning on the spot
- The grass is slow
- Steering by touch
- A track with curves
- Bend the road
- No jumps in the curve
- Which segment am I on?
- Curves pull you outwards
- Other cars
- Distance on a loop
- Cars that shrink with distance
- Traffic moves
- Rear-end crash
- Counting laps
- Show the lap
- A lap timer
- Show the time
- Best lap
- Show the best lap
- Racing or finished
- The finish line
- Finished!
- Race again
- Build it yourself: beat the clock