ActionScript & Flash
Build a predictable game timer loop
A game loop has two jobs: advance the simulation and draw the result. Timer callbacks are opportunities to do that work, not guarantees that exactly the requested number of milliseconds has passed. Measure elapsed time instead of treating a callback count as a clock.
Accumulate elapsed time and update the simulation in fixed steps. Put a ceiling on catch-up work after a pause so a slow frame does not create an endless backlog. Rendering can happen once after the updates, keeping input, movement and collision behavior consistent.
Profile the update and render phases separately. Avoid creating temporary objects inside the hottest loop, cache repeated lookups and stop timers when the game is paused. Optimization becomes useful when it removes a measured bottleneck.
Try the idea
var now:int = getTimer();
var elapsed:Number = (now - previous) / 1000;
previous = now;
accumulator += Math.min(elapsed, 0.25);
while (accumulator >= step) {
update(step);
accumulator -= step;
}
render();