About your code:
I think it's funny that you use anonymous functions for event listeners, it's something I don't see that often in ActionScript (it is more common with JavaScript).
You should be aware that you can run into some garbage-collection issues if you add event listeners like that (because you're no longer able to remove them later on). As long as your event listeners stay up during the whole execution time of your app, it isn't an issue though.
Regarding your questions:
- An update step can take longer to execute than 1/60 of a second, even if you specified the FPS to be 60. It just means it won't go above 60, but can easily drop below 60 FPS. You can test this yourself by adding some dummy loop in your event listener, something like:
for(var i:int = 0; i < 1000000; i++){ var tmp:Number = 1000.0/20.0; }
. The Flash runtime will use as much time as required for a frame. That means it will try to run at the specified framerate, but it won't enforce it in any way (eg. stopping execution of code). An endless loop (while(true){}
) will freeze your game on the first frame...
- I don't understand what you mean by "limited by time"? Are you asking whether or not to use/assume a fixed or variable timestep?
Update (regarding 2.): To make an object travel some distance in a given time, you could either use a tweening-library like tweenlite or implement it yourself like this:
// initalize this with getTimer() in your constructor or init method
private var _time:Number;
// how long the sprite should be animated.. here 3.5 seconds
private var _duration:Number = 3.5;
// event listener. needs to be bound to Event.ENTER_FRAME using addEventListener
private function handleEnterFrame(evt:Event):void {
var t:Number = getTimer();
var elapsed:Number = (t - _time) * 0.001;
_time = t;
_duration -= elapsed;
if(_duration <= 0){
// ensure that elapsed doesn't overshoot the duration
elapsed += _duration;
// remove the event listener.. this will stop executing further updates
removeEventListener(evt.type, handleEnterFrame);
}
// the sprite will travel 200 pixels per second (in the x-axis)
// so 700 pixels in total (200 * 3.5)
_sprite.x += 200 * elapsed;
}