Workshop › Rhythm Lanes › Step 16 of 35

Close enough in time

What we are doing

Nobody presses at exactly the right frame, so a hit needs a window: a press counts if the note's time is within GOOD (9) frames of now, a little early or a little late. Otherwise nothing happens and the note keeps coming.

```js
const GOOD = 9

// A key press hits the closest note in its lane, if it is close enough in time.
function press(lane) {

  if (!closest) return
  const off = Math.abs(closest.time - frame)
  if (off > GOOD) return // too early: nothing happens, and the note is still there
  closest.hit = true
```

What it means

Your turn

  1. Under LEAD write GOOD (above KEYS).
  2. Above function press(lane) { write the comment.
  3. In press, between if (!closest) return and closest.hit = true, write the two new lines.

Skills: Game states

All steps

  1. A stage for the notes
  2. The first lane
  3. Four lanes with a loop
  4. The hit line
  5. A function that draws
  6. The song as text
  7. From text to notes
  8. The clock decides where
  9. Draw the notes
  10. A clock that ticks
  11. The game loop
  12. Lanes that light up
  13. Keys for the lanes
  14. Show the keys
  15. Hit the closest note
  16. Close enough in time
  17. One press, one hit
  18. A message on the screen
  19. Say Hit
  20. Let the message go
  21. Perfect or good
  22. Keep count
  23. Missed notes
  24. Score
  25. Hits in a row
  26. The multiplier
  27. The song ends
  28. Stop the clock
  29. Accuracy
  30. The grade
  31. The results
  32. Play again
  33. Best score
  34. Lanes you can tap
  35. Build it yourself: a speed setting