PDA

View Full Version : Main Loop in AS3?



doomtoo
July 13th, 2009, 09:57 PM
I'm used to C++ game programming, but wondered the best way to create a main loop for updating all the sprites?

So:

MainLoop()
{
for all sprites
sprites[i].update();
}
?
Would I just use:
addEventListener(Event.ENTER_FRAME,onEnterFrame);
Or should I create a timer event and do the updates within that?

Is there anyway to control/override the "render" loop as well?

Sirisian
July 13th, 2009, 10:14 PM
If you want to know a solid way look into Gaffer Fix your Timestep (http://gafferongames.com/game-physics/fix-your-timestep/). Basically handle updating by finding the delta time between frames and use the theory in that article to run a fixed update step. This quickly allows your game to run at the same speed independent of the framerate basically. Apply this idea to your render also. Then set the FPS to like 120.

senocular
July 14th, 2009, 09:35 AM
just use ENTER_FRAME

Gnoll
July 14th, 2009, 09:48 AM
I would go with the timer to for a more consistent output on different computers (and platforms, wow I am struggling with flashplayer's fps on linux)

ActionScript Code:

private var fps:int = 30;
private var updateTimer:Timer = new Timer(60/fps);

private function main():void
{
updateTimer.addEventListener(TimerEvent.TIMER, onTimerHandler);
updateTimer.start():
}

private function onTimerHandler(e:TimerEvent)
{
updateStuff();
renderStuff();
}

Something like that :P (The Timer constructor is in millseconds, my brain is to tired to comprehend the basic conversion :D)
Gnoll

senocular
July 14th, 2009, 11:02 AM
Timers are based on frame rate (making them quite variable) and are not sync'd with frame rendering.

Gnoll
July 14th, 2009, 07:24 PM
They are based on frame rate? Of course not every render might be updated, but the logic should be running the same on most pcs,

Gnoll

senocular
July 14th, 2009, 07:34 PM
They are based on frame rate? Of course not every render might be updated, but the logic should be running the same on most pcs

Not exactly.

The only problem you'd have with enterFrame is if the computer can't keep up. If you're using timer instead, the computer would be in no better shape to keep up, and might even be trying to do more in a compressed time period.

Enter frame events keep you in sync with rendering which is what you want to be doing for anything related to screen updates. If the cpu can't keep up, you'll just be doing things a little slower, and steps can be taken to reduce that load if needed. For most cases its not a problem.

doomtoo
July 14th, 2009, 08:35 PM
Cool, thanks!

If I wanted to have something run less often (say AI) would the best way be to just put a timer inside the enter_Frame event?

Offtopic, does anyone know where to find the source for a flash game with a main loop? Not needed, but might be helpful to see how they implement everything.

Thanks for the help!

Gnoll
July 14th, 2009, 11:49 PM
True it will be in sync with framerate, but you probably don't want game logic depending on fps. AS3 will already render everything attached to the stage for you. If you want a main loop for logic you could use enterFrame or a timer.

Why would you use a timer in an enterFrame?
Gnoll