开发者

Trying to limit the frame-rate of my Flash game

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 wo开发者_如何学JAVAuld mean my draw calls and logic calls are 1:1 right?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜