Does AS3 Event.ENTER_FRAME run on every frame, always? Even on slow computers?
I have a script that relies on ENTER_FRAME
event to run every time. I 开发者_开发百科have noticed on some slower computers there can be some lag when a flash movie is playing.
Does ENTER_FRAME
run on every frame, even if its on a slow computer?
If the flash movie lags, does the ENTER_FRAME
event still run and the rendering just try to catch up?
Is running code on ENTER_FRAME
a reliable way to execute code every time a frame is entered?
Yep. Every frame, no exceptions. If something is slowing a movie down (either heavy scripts or heavy graphics), it Event.ENTER_FRAME
handlers are still being executed before a frame is rendered.
Hence, it's generally a good idea to use a Timer
instance with TimerEvent.TIMER
, even if it's delay is set to be equal to 'ideal' frame duration for your movie's fps. Because timer
handler is not bound to be triggered at exactly uniform rate.
See the following link for more in-depth explanation: The Elastic Racetrack
if you have a framerate set to 30fps, then the event will fire 30 times per second, as long as you don't put a load on the processor, making the frame rate drop. Therefor, if the framerate is fluctuating, you might get more consistent results with a timer Event.
on a side note, be aware that... Using many Event handlers can create performance issues too (if you have too many) Every time it is called, flash has to create an event object at the very least. That means you have memory that needs to be allocated every time the event fires. That memory then needs to be garbage collected at a later time, and the garbage collection will also use resources to execute.
If you have have many movie clips or sprites it could be worthwhile to have one controller that manages all of them, rather than each one having it's own EnterFrame handler.
The general answer to general question.
If you want to improve performance of Flash Player then consider following points,
Do not use strokes unless if it is required. (Strokes are more cpu
intensive)Use less gradient colors if possible.
Use optimized bitmaps if any.
Make effective use of
addChild(yourObject)
,addChildAt(yourObject, index)
,removeChild(yourObject)
,removeChildAt(index)
.Listen to
Event.ADDED_TO_STAGE
andEvent.REMOVED_FROM_STAGE
respectively.Listen to
addEventListener(somelistener, somefunction);
removeEventListener(somelistener, somefunction);Listen to
Event.ACTIVATE
andEvent.DEACTIVATE.
If objects are loaded externally then make sure to use
unloadAndStop()
to completely remove unnecessaryobjects
from the stage.
Check this out for anyone looking for a framerate independent solution... this guy's really smart and has a technique for both animating consistently across multiple framerates (slower devices, desktops, etc) and keeping your object's framerate independent of your timeline's framerate. Check it out here. Tips 4 & 5. Hope that helps!
I found that the timer class is actually very inconsistent when mashing buttons, sometimes the timer just fails to complete a cycle and the TIMER.COMPLETE event never gets reached, if I had 5 cycles of 100ms, it would just stop after 3 cycles... Also, framerate will fire every frame but IT IS NOT CONSISTENT!!! If you have lag on the CPU, your framerate will drop and hence you will not have anything updating at regular intervals, but rather whatever the current framerate is. Check out that link, there's even some framerate code you can use on your projects to check it.
精彩评论