Optimizing your Flash SWFs (part 2)
Caching movie clips with cacheAsBitmap
As your designs in Flash grow in size, whether you are creating an application or complex scripted animations, you need to consider performance and optimization. When you have content that remains static (such as a rectangle movie clip), Flash does not optimize the content. Therefore, when you change the position of the rectangle movie clip, Flash redraws the entire rectangle in Flash Player 7 and earlier.
In Flash Player 8, you can cache specified movie clips and buttons to improve the performance of your SWF file. The movie clip or button is a surface, essentially a bitmap version of the instance's vector data, which is data that you do not intend to change much over the course of your SWF file. Therefore, instances with caching turned on are not continually redrawn as the SWF file plays, which lets the SWF file render quickly.
You can use ActionScript to enable caching or you can use the Property inspector . To cache movie clips or buttons without using ActionScript, you can select the Use runtime bitmap caching option in the Property inspector instead. (fig1)
fig1
If you want to set that property to true with Actionscript just set it using the syntax below :
| anyMovieClip.cacheAsBitmap = true; |
Using cacheAsBitmap especially increase performance for movie clips that contain complex vector content and whose visual appearance doesn't change often or not at all. This is because when a cached movie clip or its contents change, Flash regenerates the bitmap with new data for the area or region of vector data that changed and updates the bitmap held in memory. The renderer then displays the new bitmap.
Every time you rotate, scale, or change the alpha of a cached movie clip, the whole bitmap has to be regenerated. So, turning on bitmap caching for a movie clip that is constantly rotating, changing in size, or contains an animation doesn't make much sense because in every frame the renderer has to update the bitmap to reflect the new appearance of the movie clip as well as redraw it to the Stage, which adds overhead.
On the other hand, bitmap caching works well for both static movie clips and movie clips that move, as long as they don't change visually. It is perfectly fine to move a cached movie clip around the Stage, either with ActionScript or with Timeline animation, because moving it around won't change its visual appearance. The cached bitmap doesn't have to be updated, and the renderer will merely draw the bitmap at its new position.
Even if cacheAsBitmap is set to true, The Flash player will have to regenerate the surfaces when any of the following properties change :
_xscale,
_yscale,
_rotation,
_alpha,
_width,
_height,
filters,
blendMode,
opaqueBackground,
transform
The bitmap will also be regenerated when:
-
The Timeline playhead changes inside the movie clip
-
When the outer boundaries of the movie clip change
-
When you draw something inside the movie clip with the Drawing API
- When you attach an image or symbol from the Library into the movie clip
- Any of the above occur within a nested movie clip (child movie clip)
- The movie's viewing window changes (for example, the viewer zooms in on the movie by right-clicking and choosing Zoom)
Bitmap caching naturally makes Flash Player use more memory, because for every cached movie clip the player has to store the vector data and the additional bitmap equivalent in memory. When you turn bitmap caching off for any particular movie clip, Flash removes its bitmap representation from memory. So if you don't need a Movie clip anymore at any given time you can free up some memory using the following syntax :
| anyMovieClip.cacheAsBitmap = false; |
Assets set to export on first frame will be compiled in the final SWF
Normally any asset that is not used in a Flash project will not be included in the compiled SWF but this is not true for the Adobe components and for any asset set to be exported on first frame. Therefore be sure to remove manually any of those assets from your library before compiling your SWF, you might be surprised to see your SWF's size dropping considerably. You can have a look at the Adobe note regarding that issue here
|