I usually develop games using XNA and one of the common things to do when using XNA is to make sure that you update each entity based on ElapsedGameTime. This allows smooth movement even with a variable framerate.
I am looking for that functionality in AS3 using FlashDevelop.
So far I have come up with:
public function Update():void
{
var currentTime:Number = getTimer();
var elapsedTime:Number = currentTime - lastFrame;
elapsedTime /= 1000;
//use elapsedTime here for movement
player.x += speed.x * elapsedTime
lastFrame = getTimer();
}
With using this, I cannot really tell if it is actually working or not. I don’t really see the difference when I implement this.
Is this similar? Is there another way to do this?
It seems it would be possible that you see no difference in using this versus using an enter_frame handler if it’s the case it’s probably true that all the processing needed for a given frame is happening within the allotted time and it’s actually able to update at a smoothly at 1/24th of a second (default frameRate is 24). One way to test the difference would be to modify the frameRate property and see if the speed of your “character” is affected by the change in frameRate. You can also use a trace statement like trace(elapsedTime) you could also add an enter_frame event handler and keep track of the frames and trace that out as well to get better insight into how this is performing and what if any difference it’s making. This seems like a totally valid way to go though I haven’t done a lot of Flash game programming, in having a lot of experience with Flash RIA development I don’t see anything wrong with this method.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/Application.html#frameRate
if using pure AS3 you would set this using metadata on your main class like:
http://flassari.is/2009/02/swf-metadata-in-pure-as3-projects/
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf680e1-7ffe.html#WS4b4ddee4904fe7796d38fa291219d46bcb4-8000