AS3 Timers vs. ENTER_FRAME performance

后端 未结 4 1922
有刺的猬
有刺的猬 2021-02-01 09:56

I\'m building a game that got some moving things all the time, so I\'m using a lot of Timer instances to control repetition and trigger motion.

Now the thing is that I

4条回答
  •  温柔的废话
    2021-02-01 10:26

    I'd recommend using ENTER_FRAME as the master "tick" for your game engine. ENTER_FRAME lines up exactly with the Flash Player's framerate, which is the true maximum framerate your code will run at. Timers, etc., are just approximations and cannot execute any faster than ENTER_FRAME.

    In fact, while I originally used Timers for all of my stuff, I'm slowly moving away from them because of aliasing issues. If you set your Timer for 30fps, but the Flash Player ends up running at 15fps, then the Timer will end up dispatching it's TIMER event twice between ENTER_FRAME events. If these TIMER events lead to expensive code (which they would if it's your game engine's tick), then it has the potential of pushing the Player's actual framerate lower (because now you're ticking twice per ENTER_FRAME).

    So, Timer is good if you've got something that you want to run periodically, but for running anything close to your SWF's actual framerate I'd recommend just using the SWF's framerate and adjusting your logic as necessary.

    One approach is to calculate time deltas on each ENTER_FRAME. If you've got time-based logic, this is the best approach. Another approach, if your SWF is assuming a fixed update rate (like Timer-based code), is to call your game's tick method if-and-only-if you've exceeded the time delta on any given ENTER_FRAME.

    I would not recommend doing two ticks per ENTER_FRAME if you fall behind (or you'll end up with the same situation as the Timers). At a certain point, your game has to slow down or it becomes unplayable (because the deltas get too big). Doing more than one tick per ENTER_FRAME when you're already slowed down will only slow you down further. Users can better handle slowed gameplay than they can skipping gameplay.

提交回复
热议问题