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.
// ---------- 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(); });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().
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:
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.
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.
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();
}
The game listens on ALL of them, because browsers (especially iOS Safari, especially inside an iframe) are picky about which tap signal they deliver:
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.
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.
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.