← AI & Me Studios

04 Input

How taps, clicks, and keys reach the game — including the dedupe that stops one tap counting twice, and the Space-bar fix that forwards keys from the site shell.

Code
// ---------- input ----------
function thrust() {
  audio();
  if (state === 'ready') { startGame(); fireBurn(); }
  else if (state === 'playing') { fireBurn(); }
  else if (state === 'over') {
    // Brief lockout so the tap that killed you doesn't instantly restart.
    if (overAt && performance.now() - overAt < 450) return;
    startGame(); fireBurn();
  }
  else if (state === 'clear') {
    if (overAt && performance.now() - overAt < 450) return;
    nextLevel(); fireBurn();
  }
}
let overAt = 0;
const _gameOver = gameOver;
gameOver = function (reason) { _gameOver(reason); overAt = performance.now(); };

/* Input: listen on touch, pointer AND mouse. iOS Safari is picky about
   which tap signal it delivers (especially inside an iframe), so the game
   takes all three. thrust() is idempotent, and a short dedupe window keeps
   one physical tap from counting twice (touchstart + pointerdown arrive
   milliseconds apart; real re-taps are slower). */
let lastTap = 0;
function handleTap(clientX, clientY) {
  const now = performance.now();
  if (now - lastTap < 100) return;
  lastTap = now;
  const r = cv.getBoundingClientRect();
  const x = (clientX - r.left) / r.width * W, y = (clientY - r.top) / r.height * H;
  if (x > W - 52 && y < 52) { muted = !muted; return; }
  thrust();
}
window.addEventListener('pointerdown', (e) => {
  e.preventDefault();
  handleTap(e.clientX, e.clientY);
});
window.addEventListener('touchstart', (e) => {
  e.preventDefault();
  const t = e.changedTouches[0];
  handleTap(t.clientX, t.clientY);
}, { passive: false });
window.addEventListener('touchmove', (e) => { e.preventDefault(); }, { passive: false });
window.addEventListener('touchend', (e) => { e.preventDefault(); }, { passive: false });
window.addEventListener('mousedown', (e) => { handleTap(e.clientX, e.clientY); });
window.addEventListener('keydown', (e) => {
  if (e.code === 'Space' || e.code === 'ArrowUp') { e.preventDefault(); thrust(); }
  if (e.code === 'KeyM') muted = !muted;
});
/* Keyboard bridge: the site shell forwards keys via postMessage when the
   frame doesn't hold focus (otherwise Space scrolls the parent page). */
window.addEventListener('message', (e) => {
  if ((e.origin !== 'https://aiandmestudios.com' && e.origin !== 'https://www.aiandmestudios.com' && e.origin !== 'https://ai-and-me-studios.pages.dev') || !e.data || e.data.type !== 'game-key') return;
  if (e.data.action === 'down' && (e.data.code === 'Space' || e.data.code === 'ArrowUp')) thrust();
});
document.addEventListener('visibilitychange', () => { last = performance.now(); });
What it does

This file is how the player talks to the game. It takes taps, clicks, and key presses from every source the browser offers, cleans them up, and turns them into one action: thrust().

thrust() — THE ONE ACTION

    function thrust() {
      audio();
      if (state === 'ready') { startGame(); fireBurn(); }
      else if (state === 'playing') { fireBurn(); }
      else if (state === 'over') { ... startGame(); fireBurn(); }
      else if (state === 'clear') { ... nextLevel(); fireBurn(); }
    }

thrust() does the right thing for whatever state the game is in:

  • 'ready': start the game AND fire the rockets (so the first tap both starts the run and lifts the ship).
  • 'playing': just fire the rockets.
  • 'over' / 'clear': restart (new game / next level) AND fire — but only after a 450ms lockout. That lockout is important: without it, the tap that killed you would instantly restart the game before you even saw the crash screen.

fireBurn() — STARTING A BURN

    function fireBurn() {
      burnT = BURN_TIME;
      sfx('thrust');
    }

fireBurn() sets burnT to the full burn time (0.15s) and plays the thruster sound. Every tap RESTARTS the burn at full length — the rockets always fire when tapped, and tapping fast keeps the burn going. That's why rapid tapping holds the ship up.

overAt AND THE GAME-OVER WRAPPER

    let overAt = 0;
    const _gameOver = gameOver;
    gameOver = function (reason) { _gameOver(reason);
        overAt = performance.now(); };

This is a wrapper: it saves the original gameOver, then replaces it with a version that does the same thing AND records the exact time the run ended (overAt). thrust() uses overAt for the 450ms lockout. It's a small trick — instead of editing gameOver itself, the code wraps it.

handleTap() — CLEANING UP A TAP

    let lastTap = 0;
    function handleTap(clientX, clientY) {
      const now = performance.now();
      if (now - lastTap < 100) return;
      lastTap = now;
      ...
      if (x > W - 52 && y < 52) { muted = !muted; return; }
      thrust();
    }
  • DEDUPE: if less than 100ms has passed since the last tap, ignore this one. iOS Safari can deliver the same physical tap twice (touchstart AND pointerdown, milliseconds apart). Real re-taps are slower than 100ms, so this only kills duplicates.
  • COORDINATE CONVERSION: the tap arrives in screen pixels; the code converts it to logical playfield coordinates (0–480, 0–640) using the canvas's bounding rectangle.
  • MUTE BUTTON: if the tap lands in the top-right corner (x > W-52, y < 52), it toggles mute instead of thrusting. That's the speaker icon in the HUD. It returns early so a mute tap never fires the rockets.
  • Otherwise: thrust().

THE LISTENERS — EVERY WAY TO TAP

The game listens on ALL of them, because browsers (especially iOS Safari, especially inside an iframe) are picky about which tap signal they deliver:

  • pointerdown: the modern unified event (mouse, pen, touch).
  • touchstart: the touch-specific event, with { passive: false } so preventDefault() works.
  • touchmove / touchend: preventDefault() only — stops the page from scrolling or zooming when the player drags a finger.
  • mousedown: the mouse event, for desktop.
  • keydown: Space or ArrowUp thrusts; M toggles mute. preventDefault() stops Space from scrolling the page.

Because thrust() is idempotent (tapping twice in the same instant does the same thing as tapping once — it just restarts the burn) and the 100ms dedupe kills duplicates, listening on all of these is safe.

THE POSTMESSAGE BRIDGE — THE SPACE-BAR FIX

    window.addEventListener('message', (e) => {
      if ((e.origin !== 'https://aiandmestudios.com' &&
           e.origin !== 'https://www.aiandmestudios.com' &&
           e.origin !== 'https://ai-and-me-studios.pages.dev') ||
          !e.data || e.data.type !== 'game-key') return;
      if (e.data.action === 'down' &&
          (e.data.code === 'Space' || e.data.code === 'ArrowUp')) thrust();
    });

This is the fix for the Space-bar bug from October 2026. When the game runs inside the AI & Me Studios site, the game frame doesn't always hold keyboard focus — so pressing Space scrolls the parent page instead of thrusting. The fix: the site shell listens for keys and forwards them into the game with postMessage. The game only accepts messages from its own three origins (the two aiandmestudios.com hosts and the Pages dev host) and only for game-key events. Anything else is ignored.

THE VISIBILITYCHANGE RESET

    document.addEventListener('visibilitychange', () => {
        last = performance.now(); });

When the tab is hidden and shown again, the frame timer (last, in file 07) is reset so the next frame's dt doesn't jump by seconds. Without this, coming back to the tab would teleport the ship.