Flash Platform technology optimization (14) Freezing and unfreezing objects

Source: Internet
Author: User
Tags addchild

Use the removed_from_stage and added_to_stage events to correctly freeze and unfreeze objects.
To optimize your code, always freeze and unfreeze your objects. Freezing and unfreezing are important to all objects, but especially to display objects. Even if the Display object is no longer in the display list and is waiting for garbage collection, its code still occupies a large amount of CPU. For example, they are still using event. enter_frame. Therefore, it is critical to use the event. removed_from_stage and event. added_to_stage events to correctly freeze and unfreeze objects. The following example shows a video clip that is played on the stage and interacts with the keyboard:
// Listen to Keyboard Events
Stage. addeventlistener (keyboardevent. key_down, keyisdown );
Stage. addeventlistener (keyboardevent. key_up, keyisup );
// Create object to store key states
VaR keys: dictionary = New Dictionary (true );
Function keyisdown (E: keyboardevent): void
{
// Remember that the key was pressed
Keys [E. keycode] = true;
If (E. keycode = keyboard. Left | E. keycode = keyboard. Right)
{
Runningboy. Play ();
}
}
Function keyisup (E: keyboardevent): void
{
// Remember that the key was released
Keys [E. keycode] = false;
For each (VaR value: Boolean in keys)
If (value) return;
Runningboy. Stop ();
}
Runningboy. addeventlistener (event. enter_frame, handlemovement );
Runningboy. Stop ();
VaR currentstate: Number = runningboy. scalex;
VaR speed: Number = 15;
Function handlemovement (E: Event): void
{
If (Keys [keyboard. Right])
{
E. currenttarget. x + = speed;
E. currenttarget. scalex = currentstate;
} Else if (Keys [keyboard. Left])
{
E. currenttarget. X-= speed;
E. currenttarget. scalex =-currentstate;
}
}


After you click the delete button, the video clip will be deleted from the display list:
// Show or remove running boy
Showbtn. addeventlistener (mouseevent. Click, showit );
Removebtn. addeventlistener (mouseevent. Click, removeit );
Function showit (E: mouseevent): void
{
Addchild (runningboy );
}
Function removeit (E: mouseevent): void
{
If (contains (runningboy) removechild (runningboy );
}
Even if you delete a video clip from the display list, it still schedules the event. enter_frame event. Video Clips are still running but not rendered. Correct Handling
In this case, listen for the correct event and delete the event listener to prevent execution of code that occupies a large amount of CPU resources:
// Listen to event. added_to_stage and event. removed_from_stage
Runningboy. addeventlistener (event. added_to_stage, activate );
Runningboy. addeventlistener (event. removed_from_stage, deactivate );
Function activate (E: Event): void
{
// Restart everything
E. currenttarget. addeventlistener (event. enter_frame, handlemovement );
}
Function deactivate (E: Event): void
{
// Freeze the running boy-consumes fewer CPU resources when not shown
E. currenttarget. removeeventlistener (event. enter_frame, handlemovement );
E. currenttarget. Stop ();
}
After you press the Show button, the video clip is restarted. It listens for the event. enter_frame event again and the keyboard controls the video clip correctly.

Note: If a display object is deleted from the display list, the object will be frozen when its reference is set to null. If you do not run the garbage collector, the object will continue to occupy the memory and CPU for processing, even if the object is no longer displayed. To minimize the CPU usage of an object, make sure that the object is completely frozen after it is deleted from the display list.
In Flash Player 10 and later versions, if the playback header encounters an empty frame, the display object is automatically frozen, even if no freezing is performed. The concept of freeze is also important when using the loader class to load remote content. When using the loader class in Flash Player 9, The loaderinfo
The event. Unload event scheduled by the object to manually freeze the content. Each object must be manually frozen, which is a crucial task. Flash Player 10 introduces an important new method to the loader class, called unloadandstop (). This method can be used to unmount the SWF file and automatically freeze the loaded SWF file.
And forcibly run the garbage collector.
The following code loads the SWF file and unloads it using the unload () method. This requires more processing operations and manual freezing:
VaR Loader: loader = new loader ();
Loader. Load (New URLRequest ("content.swf "));
Addchild (loader );
Stage. addeventlistener (mouseevent. Click, unloadswf );
Function unloadswf (E: mouseevent): void
{
// Unload the SWF file with no automatic object deactivation
// All deactivation must be processed manually
Loader. Unload ();
}
It is best to use the unloadandstop () method, which processes the freeze and forcibly runs the garbage collection process on the local machine:
VaR Loader: loader = new loader ();
Loader. Load (New URLRequest ("content.swf "));
Addchild (loader );
Stage. addeventlistener (mouseevent. Click, unloadswf );
Function unloadswf (E: mouseevent): void
{
// Unload the SWF file with Automatic Object deactivation
// All deactivation is handled automatically
Loader. unloadandstop ();
}
When the unloadandstop () method is called, the following operations are performed:
• Stop sound.
• The Listener registered to the SWF file will be deleted.
• Stop the timer object.
• Release hardware peripherals (such as cameras and microphones ).
• Stop each video clip.
• Stop scheduling event. enter_frame, event. frame_constructed, event. exit_frame, event. Activate, and
Event. Deactivate.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.