Workshop › Pinball › Step 21 of 42
Remember which bumper was hit
What we are doing
A hit bumper should light up for a moment. We keep one number per bumper in flash: how many more frames it stays
lit. A hit sets it to 10.
```js
let flash // frames each bumper stays lit
flash = BUMPERS.map(() => 0)
function hitBumper(b, i) {
flash[i] = 10
```
What it means
BUMPERS.map(() => 0) makes a new list with a 0 for every bumper: [0, 0, 0].
forEach gives the function a second value, the index i (0, 1, 2), so hitBumper(b, i) knows which bumper it is.
flash[i] = 10 lights bumper number i for 10 frames.
Your turn
- Under
let score write let flash.
- In
reset, under score = 0, write the flash line.
- Add
, i to hitBumper's parameters, and write flash[i] = 10 at its end.
Skills: Arrays and objects, Game states
← Points for every hit Light it up →
All steps
- Paint the table
- Draw one wall
- A list of walls
- The game loop
- A ball in the lane
- Draw the ball
- Move by the velocity
- Launch with Space
- Gravity
- Does the ball touch a wall?
- Push the ball out
- Bounce off the wall
- Small steps and a speed limit
- Ready or playing
- The next ball
- Three bumpers
- The ball meets a bumper
- A bumper kicks
- A score and a fresh start
- Points for every hit
- Remember which bumper was hit
- Light it up
- Lit for a moment, points once
- Two flippers
- Where is the tip?
- Flippers stop the ball
- Which keys are held?
- Which way to turn
- Flip!
- A moving wall
- The flipper throws
- Three balls
- Balls left and game over
- Play again
- Remember the best score
- Launch on letting go
- Pull the spring
- Show the spring
- Two thumbs
- Pull with a finger
- Tap to play again
- Build it yourself: slingshots