Workshop › Puzzle Bobble-style Bubble Shooter › Step 15 of 31

Smaller steps

What we are doing

At 12 pixels a frame, a shot can jump over a narrow gap or deep into a bubble before we check. So each frame is split into three smaller steps of 4 pixels, each checking for a touch.

```js
// A few small steps per frame, so the bubble cannot jump past a gap.
for (let i = 0; i < 3 && shot; i++) {
  shot.x += shot.vx / 3
  shot.y += shot.vy / 3
  if (shot.x < R || shot.x > canvas.width - R) {
    shot.vx = -shot.vx // bounce off the side walls
    shot.x = Math.max(R, Math.min(canvas.width - R, shot.x))
  }
  if (touches(shot.x, shot.y)) {
    const cell = snap(shot.x, shot.y)
    attach(cell.r, cell.c, shot.color)
    shot = null
  }
}
```

What it means

Your turn

In update, put the lines after if (!shot) return inside the for loop (with its comment), indented, and divide the velocities by 3.

Skills: Velocity and gravity, Collision detection

All steps

  1. The night sky
  2. One bubble
  3. A grid of cells
  4. Draw the grid
  5. Bubbles that nest
  6. The shooter
  7. Aim upwards
  8. Turn with the arrows
  9. Aim with the pointer
  10. Fire!
  11. Fly
  12. Bounce off the walls
  13. Touching something?
  14. Stick into the grid
  15. Smaller steps
  16. Six neighbours
  17. Flood fill
  18. Three in a row pop
  19. Bubbles that fall
  20. Cut them loose
  21. Show the score
  22. Only useful colors
  23. Count the shots
  24. The ceiling comes down
  25. Win or lose
  26. The danger line
  27. The end message
  28. Play again
  29. Best score
  30. A dotted guide
  31. Build it yourself: swap bubbles