AS3-下一帧/屏幕绘制需要多少时间 [英] AS3 - How much time until next frame / screen draw

查看:117
本文介绍了AS3-下一帧/屏幕绘制需要多少时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成艺术应用程序,我希望它在不降低帧频的情况下每帧绘制尽可能多的周期.有没有办法告诉屏幕更新/刷新还剩下多少时间?

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.

推荐答案

如果您希望您的应用以每秒N帧的速度运行,则可以画一个循环,持续1/N秒*,其中N通常是阶段帧率(您可以获取并设置):

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
  }
}

  • 请注意,这有点简化,并且可能导致最终的帧速率比stage.frameRate指定的帧速率慢,因为Flash需要一些时间才能在帧之间进行渲染.但是,如果您是在挥洒(在屏幕上绘制为位图)而不是在矢量中绘制或在屏幕上添加Shapes,那么我认为以上内容实际上应该是非常准确的.
  • 如果代码导致帧速率慢于期望的速率,则可以尝试一些简单的事情,例如仅花费一帧分配的时间的一半,而将另一半留给Flash渲染:

    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;
    

    周围还有一些FPS监视器,可用于在绘制时监视帧速率. Google as3帧率监控器.

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

    这篇关于AS3-下一帧/屏幕绘制需要多少时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆