AS3 - How much time until next frame / screen draw

痴心易碎 提交于 2019-12-08 01:32:42

问题


I have a generative art app, and I'd like it to draw as many cycles as possible each frame without reducing the framerate. Is there a way to tell how much time is left until the screen updates/refreshes?

I figure if I can approximate how many milliseconds each cycle takes, then I can run cycles until the amount of time left is less than the average or the peak cycle time, then let the screen refresh, then run another set of cycles.


回答1:


If you want your app to run at N frames per second, then you can draw in a loop for 1/N seconds*, where N is typically the stage framerate (which you can get and set):

import flash.utils.getTimer;
import flash.events.Event;

private var _time_per_frame:uint;

... Somewhere in your main constructor:

stage.frameRate = 30;
_time_per_frame = 1000 / stage.frameRate;
addEventListener(Event.ENTER_FRAME, handle_enter_frame);

...

private function handle_enter_frame(e:Event):void
{
  var t0:uint = getTimer();

  while (getTimer()-t0 < _time_per_frame) {
    // ... draw some stuff
  }
}
  • Note that this is somewhat of a simplification, and may cause a slower resultant framerate than specified by stage.frameRate, because Flash needs some time to perform the rendering in between frames. But if you're blitting (drawing to a Bitmap on screen) as opposed to drawing in vector or adding Shapes to the screen, then I think the above should actually be pretty accurate.

If the code results in slower-than-desired framerates, you could try something as simple as only taking half the allotted time for a frame, leaving the other half for Flash to render:

_time_per_frame = 500 / stage.frameRate;

There are also FPS monitors around that you could use to monitor your framerate while drawing. Google as3 framerate monitor.




回答2:


Put this code to main object and check - it will trace time between each frame start .

addEventListener(Event.ENTER_FRAME , oef);
var step:Number = 0;
var last:Number = Date.getTime();
function oef(e:Event):void{
    var time:Number = Date.getTime();
    step = time - last;
    last = time;

    trace(step);
}


来源:https://stackoverflow.com/questions/9264681/as3-how-much-time-until-next-frame-screen-draw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!