AS3 - 距离下一帧/屏幕绘制还有多长时间

发布于 2025-01-04 22:35:05 字数 140 浏览 1 评论 0原文

我有一个生成艺术应用程序,我希望它在每帧中绘制尽可能多的周期而不降低帧速率。有没有办法知道屏幕更新/刷新还剩多少时间?

我想如果我可以估计每个周期需要多少毫秒,那么我可以运行周期直到剩余时间少于平均或峰值周期时间,然后让屏幕刷新,然后运行另一组周期。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福还没到 2025-01-11 22:35:05

如果您希望您的应用程序以每秒 N 帧的速度运行,那么您可以循环绘制 1/N 秒*,其中 N 通常是阶段帧速率(您可以获取和设置):

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

private var _time_per_frame:uint;

...构造函数:

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 需要一些时间来在帧之间执行渲染。但是,如果您使用位图传输(在屏幕上绘制位图)而不是在矢量中绘制或将形状添加到屏幕上,那么我认为上面的内容实际上应该非常准确。

如果代码导致帧速率慢于所需的帧速率,您可以尝试一些简单的方法,例如仅花费一帧分配时间的一半,将另一半留给 Flash 渲染:

_time_per_frame = 500 / stage.frameRate;

周围还有 FPS 监视器,您可以使用它们来监视绘图时的帧速率。谷歌 as3 帧率监视器

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.

宣告ˉ结束 2025-01-11 22:35:05

将此代码放入主对象并检查 - 它将跟踪每个帧开始之间的时间。

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文