AI GAME GENERATOR · RESTART LIFECYCLE
Why Does My Game Get Faster Every Time I Restart?
Check whether Restart starts another update loop while the old one keeps running. Give the loop one owner, cancel its latest pending animation-frame request before restarting, and reset the round state separately. If your engine already owns the loop, use its scene restart lifecycle instead of adding another loop.
An AI game generator can produce a playable first round while leaving restart cleanup incomplete. The useful clue is the pattern: a fresh page feels normal, one restart feels faster, and a second restart makes it worse. That pattern is a reason to inspect scheduling, not proof that scheduling is the only cause.
What should you count first?
Record update calls over the same short interval before and after Restart, alongside movement distance and the configured speed. Count callbacks, not just drawn frames: several callbacks can run before one repaint. Check for extra requestAnimationFrame chains, repeated timers, or duplicated input subscriptions. Do not lower the speed value until you know which count changed.
MDN documents requestAnimationFrame as one-shot: a recurring loop requests its next frame from inside the callback. Each request returns an identifier. cancelAnimationFrame cancels a pending request; save the new identifier every time you schedule, rather than keeping only the first one.
An isolated restart experiment: one owner versus three chains
We built a small callback-queue model in Node.js. It has no graphics, collision engine, browser refresh rate, or network connection. A synthetic repaint consumes the callbacks already queued; callbacks scheduled during that step wait for the next repaint. Every callback increments one counter and schedules its successor. This makes lifecycle ownership visible without confusing it with hardware performance.
The broken Restart schedules a new chain without removing the old one. The corrected Restart cancels the latest pending request before scheduling one replacement. Before each measurement, the model advances the existing chains, so cancelling a stale first identifier would not be sufficient. We then count updates across sixty synthetic repaints. All six runs use the same measurement length.
| Restarts after initial start | Uncleaned: updates / 60 steps | Owned loop: updates / 60 steps |
|---|---|---|
| 0 | 60 | 60 |
| 1 | 120 | 60 |
| 2 | 180 | 60 |
After two restarts, the broken model still has three pending callbacks. The corrected model has one. Stopping the corrected model leaves zero pending callbacks. These results demonstrate what these two implementations do; they do not establish an exact speed multiplier for your game. A game using shared timestamps, a physics accumulator, or an engine-owned ticker can respond differently.
Separate resetting a round from starting a scheduler
Give Restart an explicit sequence: stop the owned scheduler, reset round-only state, clear the previous timestamp, and schedule one new callback. Keep persistent settings outside that reset. Bind the Restart button once during setup, rather than adding a fresh anonymous listener every time the round starts. Resetting position and score does not automatically remove a pending callback.
For a hand-written loop, store the most recent request identifier in one variable and use null when no request is owned. In the frame callback, update that variable whenever you request another frame. In Stop, cancel the currently pending identifier and clear ownership. Avoid simultaneously running a custom loop and an engine's ticker to update the same world.
Use a restart ledger instead of guessing
Write down four checkpoints: fresh start, first restart, second restart, and stopped. At each checkpoint, record the interval length, update count, speed setting, and the movement you can actually observe. Keep the scene and input sequence unchanged. If callback counts remain stable but movement increases, inspect reset values, duplicated forces, or repeated input handlers rather than declaring the loop fixed or broken from appearance alone.
Test a second Restart pressed quickly after the first. It should still leave one scheduler owner. Then Stop and check that no updates continue. Finally restart from a stopped state. Our queue model checks cancellation and counts only; it does not test rapid real clicks, browser throttling, mobile devices, engine scene hooks, or a generated product. Run those checks on the actual build before claiming they pass.
A repair prompt for your AI game generator
After a fresh page load, movement is normal. After Restart, movement becomes faster. Inspect every path that starts the update loop, timer, and input listeners. Keep one scheduler owner; cancel its latest pending request before replacing it, and reset the round timestamp. If the engine owns the loop, use its existing lifecycle. Preserve speed, gravity, controls, and scoring rules. Show the changed lifecycle code and report update counts for fresh start, two restarts, and Stop. Do not claim tests you did not run.
Questions to resolve before publishing
Will cancelAnimationFrame stop a loop that already ran?
It cancels the pending request identified by its argument. It does not undo an update that already happened or remove every chain automatically. Store the latest identifier and prevent unintended paths from scheduling another successor.
What if the game is faster on a different monitor?
That can be a different timing problem. A fixed distance per callback depends on callback frequency. Use elapsed time from the animation timestamp for movement, following the engine's timing model. First distinguish hardware refresh differences from counts that increase after restarting the same scene.
Should I rewrite the whole game?
Start with a bounded lifecycle repair. Keep a copy of the working build, change scheduler ownership, and repeat the same checks. For a structured request, use the one-bug repair guide. For a hidden-tab timing policy, see pause when switching tabs; for release distance, see floaty movement checks.
Watch the illustrated explanation of the same synthetic restart model.