Select a timer or enter_frame event based on whether the content is animated.
For non-animated content that has been executed for too long, the timer is preferred, instead of the event. enter_frame event.
In ActionScript 3.0, there are two ways to call a function at a specific interval. The first method is to use the event. enter_frame event scheduled by the interactive object (interactiveobject. The second method is to use a timer. The enter_frame event method is usually used by ActionScript developers. You need to schedule enter_frame events for each frame. Therefore, the call interval of the function is related to the current frame rate. You can view the frame rate through the stage. framerate attribute. However
In some cases, using a timer is more appropriate than using an enter_frame event. For example, if you do not use an animation but want to call it at a specific intervalCodeThe timer may be a better choice.
The timer behavior is similar to that of the enter_frame event, but the frame rate is not required for scheduling events. Through this behavior, some important optimizations can be achieved. Video Player applicationsProgramFor example. In this case, the high frame rate is not required because only application controls are moved.
Note: The frame rate does not affect the video because the video is not embedded in the timeline. Videos are dynamically loaded through progressive download or stream download.
In this example, the frame rate is set to a lower value of 10 FPS. The timer updates the control once per second. The updateafterevent () method provided in the timerevent object can provide a higher update rate. If needed, this method will force update the screen each time the timer schedules an event. The following code demonstrates
The concept is as follows:
// Use a low frame rate for the Application
Stage. framerate = 10;
// Choose one update per second
VaR updateinterval: Int = 1000;
VaR mytimer: timer = new timer (updateinterval, 0 );
Mytimer. Start ();
Mytimer. addeventlistener (timerevent. Timer, updatecontrols );
Function updatecontrols (E: timerevent): void
{
// Update controls here
// Force the controls to be updated on screen
E. updateafterevent ();
}
Calling the updateafterevent () method does not change the frame rate. It only forces Flash Player to update the changed content on the screen. The timeline is still running at 10 FPS. Remember that when low-performance devices or event processing functions contain code that requires a large amount of processing, the timer and enter_frame events are not completely
Accurate. As with the SWF file frame rate, the update Frame Rate of the timer varies with the actual situation.
Minimize the number of timer objects and registered enterframe handler functions in the application.
For each frame, an enterframe event is scheduled for each Display object in the displayed list at runtime. Although you can use multiple display objects as enterframe
Event registers the listener, but this means that more code will be executed on each frame. Alternatively, consider using a centralized enterframe processing function that runs all the code required for each frame. This type of code is centralized to make it easier to manage all frequently-Running code.
Similarly, if a timer object is used, overhead associated with creating and scheduling events from multiple timer objects will be generated. If you have to trigger different operations at different time intervals, the following are recommended alternatives:
• Use a minimum number of timer objects and group operations based on the frequency of occurrence
For example, you can set a timer object to be triggered every 100 milliseconds for frequent operations. Use another timer object for low-frequency operations or background operations and set it to trigger every 2000 milliseconds.
• Use a timer object and trigger the operation at a multiple of the delay attribute interval of the timer object.
For example, assume that you want some operations to take place every 100 milliseconds, while others will take place every 200 milliseconds. In this case, use a timer object with a delay value of 100 milliseconds. In the timer event processing function, add a Condition Statement, that is, the operation that runs at an interval of 200 milliseconds only once. The following example demonstrates this technology:
VaR Timer: timer = new timer (100 );
Timer. addeventlistener (timerevent. Timer, timerhandler );
Timer. Start ();
VaR offcycle: Boolean = true;
Function timerhandler (Event: timerevent): void
{
// Do things that happen every 100 MS
If (! Offcycle)
{
// Do things that happen every 200 MS
}
Offcycle =! Offcycle;
}
Stop unused timer objects.
If the timer event processing function of the timer object performs operations only under specific conditions, when these conditions are not met, the STOP () method of the timer object is called. In the enterframe event or timer processing function, minimize changes to the appearance of the display object that can cause the screen to be repainted.
for each frame, the presentation phase redraws the stage Section changed during the frame. If the re-painting area is large or small but contains a large number of or complex display objects, it takes more time to render the display during running. To test the amount of data to be repainted, use the "display repainted area" function in Flash Player debugging or air.
For more information, see Article :
• writing well-known-behaved, efficient, AIR applications (articles and sample applications written by Arno gourdol)