尝试限制我的 Flash 游戏的帧速率

发布于 2024-09-30 20:46:00 字数 427 浏览 4 评论 0原文

我有一个 2008 年制作的 Flash 游戏。现在它运行得非常快。事实上太快了。我在 FlashDevelop 中将其设置为 60fps,但我认为这只是限制了绘制调用的数量。我认为这些天我的逻辑每秒执行超过 60 次。我已经有一段时间没有使用 ActionScript 了,但我注意到我正在使用一个 EnterFrameHandler 来执行我的逻辑循环。似乎没有对其设置任何约束。我相信它只是像它所谓的那样开火。有什么办法可以将其限制在 30 或 60 fps 吗?我将非常感谢任何帮助或想法。如果逻辑运行太快,我的游戏就会被毁:(

更新 当我回想起一些 ActionScript 知识时,我想到了一些事情。 EnterFrameHandler 是否不受项目属性下 Flash 或 FlashDevelop 中 fps 设置的限制?有人能证实这一点吗?这意味着我的绘制调用和逻辑调用是 1:1 对吗?

I have a Flash game I made way back in 2008. It runs super fast these days. Way too fast in fact. I have it set to 60fps in FlashDevelop, but I think that is just limiting the amount of draw calls. I think my logic is executing well past 60 times a second these days. I haven't done ActionScript in a while, but I noticed that I am using an enterFrameHandler that executes my logic loop. It seems to have no constraint set on it. It just fires away as it is called I believe. Is there any way I can cap it at 30 or 60fps? I would greatly appreciate any help or ideas. My game is ruined if the logic runs too fast :(

UPDATE
As some ActionScript knowledge is coming back to me I just thought of something. Isn't the enterFrameHandler bound by what the fps is set to in Flash or FlashDevelop under project properties? Can anybody confirm this? It would mean my draw calls and logic calls are 1:1 right?

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

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

发布评论

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

评论(1

仙女 2024-10-07 20:46:00

事实上,enterFrame 处理程序仅在渲染帧时才会被调用(在您的情况下为 60Hz)。

如果你可以通过测量时间来限制某件事执行的次数。像这样的事情:

// A property to store the last time the code was executed
protected var lastTimeCalled:int;

// Inside your enterFrame handler
protected function onEnterFrameHandler(e:Event): void {
    var ti:int = getTimer(); // Get current time
    var desiredFPS:Number = 60; // Actual framerate you want
    var frameInterval:Number = 1000 / desiredFPS; // Desired ms per frame

    if (isNaN(lastTimeCalled) || ti >= lastTimeCalled + frameInterval) {
        // Execute code here
        ...

        // Increase time (by chunks of fixed size for framerate self-adjustment)
        lastTimeCalled = isNaN(lastTimeCalled) ? ti : lastTimeCalled + frameInterval;
    }
}

但是请注意,如果您的 SWF 的帧速率设置为 60,则尝试将desiredFPS 设置为 60 不会有任何作用;它可能只会导致一些帧丢失。不过,这可能是测试您的 EnterFrame 处理程序是否在其他地方被调用的好方法。

Indeed, enterFrame handlers are only called when it's rendering the frame (at 60Hz in your case).

If you can limit the number of times something is executed by measuring the time. Something like this:

// A property to store the last time the code was executed
protected var lastTimeCalled:int;

// Inside your enterFrame handler
protected function onEnterFrameHandler(e:Event): void {
    var ti:int = getTimer(); // Get current time
    var desiredFPS:Number = 60; // Actual framerate you want
    var frameInterval:Number = 1000 / desiredFPS; // Desired ms per frame

    if (isNaN(lastTimeCalled) || ti >= lastTimeCalled + frameInterval) {
        // Execute code here
        ...

        // Increase time (by chunks of fixed size for framerate self-adjustment)
        lastTimeCalled = isNaN(lastTimeCalled) ? ti : lastTimeCalled + frameInterval;
    }
}

Notice, though, that if your SWF's framerate is set to 60, trying to set desiredFPS to 60 won't do a thing; it'll probably just cause some frames to be dropped. It may be a good way to test if your enterFrame handler is getting called somewhere else though.

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