Adobe SWFOptimization Guidelines
Adobe SWFOptimization Guidelines
SWF file performance is important, and you can improve performance when you create your FLA document—from how you write
ActionScript code, to how you build animations. This section provides guidelines and practices that help improve the performance of your
SWF file at runtime.
Avoid using gradients, because they require many colors and calculations to be processed, which is more difficult for a computer
processor to render. For the same reason, keep the amount of alpha or transparency you use in a SWF file to a minimum. Animating
objects that include transparency is processor-intensive and should be kept to a minimum. Animating transparent graphics over bitmaps is
a particularly processor-intensive kind of animation, and must be kept to a minimum or avoided completely.
Note: The best bitmap format to import into Flash is PNG, which is the native file format of Macromedia Fireworks from Adobe. PNG files
have RGB and alpha information for each pixel. If you import a Fireworks PNG file into Flash, you retain some ability to edit the graphic
objects in the FLA file.
Optimize bitmaps as much as possible without overcompressing them. A 72-dpi resolution is optimal for the web. Compressing a bitmap
image reduces file size, but compressing it too much compromises the quality of the graphic. Check that the settings for JPEG quality in the
Publish Settings dialog box do not overcompress the image. If your image can be represented as a vector graphic, this is preferable in
most cases. Using vector images reduces file size, because the images are made from calculations instead of many pixels. Limit the
number of colors in your image as much as possible while still retaining quality.
Note: Avoid scaling bitmaps larger than their original dimensions, because it reduces the quality of your image and is processor-intensive.
Set the _visible property to false instead of changing the _alpha level to 0 or 1 in a SWF file. Calculating the _alpha level for an
instance on the Stage is processor-intensive. If you disable the instance's visibility, it saves CPU cycles and memory, which can give your
SWF files smoother animations. Instead of unloading and possibly reloading assets, set the _visible property to false, which is much
less processor-intensive.
Try to reduce the number of lines and points you use in a SWF file. Use the Optimize Curves dialog box (Modify > Shape > Optimize) to
reduce the number of vectors in a drawing. Select the Use Multiple Passes option for more optimization. Optimizing a graphic reduces file
size, but compressing it too much compromises its quality. However, optimizing curves reduces your file size and improves SWF file
performance. There are third-party options available for specialized optimization of curves and points that yield different results.
You have several options if you want to animate content in a Flash document. Animation that uses ActionScript can produce better
performance and smaller file size than animation that uses tweens at times, but sometimes not. To get the best results, try different ways of
producing an effect, and test each of the options.
A higher frame rate (measured in frames per second) produces smooth animation in a SWF file but it can be processor intensive,
particularly on older computers. Test your animations at different frame rates to find the lowest frame rate possible. For detailed
information, see "Animation Frame Rate and Performance".
For information on best practices and video, see "Video Conventions". For an example of scripted animation, see the animation.fla
example in the Samples and Tutorials/Samples/ActionScript/Animation directory within the Flash installation folder.
However, you also need to consider the frame rate setting, because it affects how smoothly your animation plays. For example, an
animation set to 12 frames per second (fps) in the Property inspector plays 12 frames each second. If the document's frame rate is set to 24
fps, the animation appears to animate more smoothly than if it ran at 12 fps. However, your animation at 24 fps also plays much faster than
it does at 12 fps, so the total duration (in seconds) is shorter. Therefore, if you need to make a 5-second animation using a higher frame
rate, it means you need to add additional frames to fill those five seconds than at a lower frame rate (and thus, this raises the total file size
of your animation). A 5-second animation at 24 fps typically has a higher file size than a 5-second animation at 12 fps.
Note: When you use an onEnterFrame event handler to create scripted animations, the animation runs at the document's frame rate,
similar to if you created a motion tween on a timeline. An alternative to the onEnterFrame event handler is setInterval. See setInterval
Function in Flash Help or the Flash 8 LiveDocs (ActionScript 2.0 Language Reference > ActionScript Language Elements > Global
Functions > setInterval Function). Instead of depending on frame rate, you call functions at a specified interval. Like onEnterFrame, the
more frequently you use setInterval to call a function, the more resource intensive the animation is on your processor.
Use the lowest possible frame rate that makes your animation appear to play smoothly at runtime, which helps reduce the strain on the
end-user's processor. Try not to use a frame rate that's more than 30 to 40 fps; high frame rates put a lot of stress on processors, and do not
change the appearance of the animation much or at all at runtime.
Also, especially if you're working with timeline-based animation, select a frame rate for your animation as early as possible in the
development process. When you test the SWF file, check the duration, and the SWF file size, of your animation. The frame rate greatly
affects the speed of the animation.
You can also encounter errors if you use invalid parameter types. Some filter parameters also have a particular valid range. If you set a
value that's outside of the valid range, the value changes to a valid value that's within the range. For example, quality should be a value
from 1 to 3 for a standard operation, and can only be set to 0 to 15. Anything higher than 15 is set to 15. Also, some constructors have
restrictions on the length of arrays required as input parameters. If a convolution filter or color matrix filter is created with an invalid array
(not the right size), the constructor fails and the filter is not created successfully. If the filter object is then used as an entry on a movie clip's
filters array, it is ignored.
Tip: When using a blur filter, using values for blurX and blurY that are powers of 2 (such as 2, 4, 8, 16, and 32) can be computed faster and
give a 20% to 30% performance improvement.
Note: The bitmap is copied to the main Stage as unstretched, unrotated pixels snapped to the nearest pixel boundaries. Pixels are
mapped one-to-one with the parent object. If the bounds of the bitmap change, the bitmap is re-created instead of being stretched.
For detailed information on caching button or movie clip instances see the following sections in Flash Help or the Flash 8 LiveDocs:
• About Caching and Scrolling Movie Clips with ActionScript (Learning ActionScript 2.0 in Flash > Working with Movie Clips > About
Caching and Scrolling Movie Clips with ActionScript)
• Caching a Movie Clip (Learning ActionScript 2.0 in Flash > Working with Movie Clips > About Caching and Scrolling Movie Clips
with ActionScript > Caching a Movie Clip)
It's ideal to use the cacheAsBitmap property with movie clips that have mostly static content and that do not scale and rotate frequently.
With such movie clips, using the cacheAsBitmap property can lead to performance improvements when the movie clip is translated (when
its x and y position is changed). Enabling caching for a movie clip creates a surface, which has several advantages, such as helping
complex vector animations to render fast. There are several scenarios in which you will want to enable caching. It might seem as though
you will always want to enable caching to improve the performance of your SWF files; however, there are situations in which enabling
caching does not improve performance, or even decrease it. The following section describes scenarios in which caching should be used,
and when to use regular movie clips. Overall performance of cached data depends on how complex the vector data of your instances are,
how much of the data you change, and whether or not you set the opaqueBackground property. If you are changing small regions, the
difference between using a surface and using vector data could be negligible. You might want to test both scenarios with your work before
you deploy the application.
• Complex background image: An application that contains a detailed and complex background image of vector data (perhaps an
image where you applied the trace bitmap command, or artwork that you created in Adobe Illustrator). You might animate characters
over the background, which slows the animation because the background needs to continuously regenerate the vector data. To
improve performance, you can select the content, store it in a movie clip, and set the opaqueBackground property to true. The
background is rendered as a bitmap and can be redrawn quickly, so that your animation plays much faster.
• Scrolling text field: An application that displays a large amount of text in a scrolling text field. You can place the text field in a movie
clip that you set as scrollable with scrolling bounds (the scrollRect property). This enables fast pixel scrolling for the specified
instance. When a user scrolls the movie clip instance, Flash shifts the scrolled pixels up and generates the newly exposed region
instead of regenerating the entire text field.
• Windowing system: An application with a complex system of overlapping windows. Each window can be open or closed (for
example, web browser windows). If you mark each window as a surface (set the cacheAsBitmap property to true), each window is
isolated and cached. Users can drag the windows so that they overlap each other, and each window doesn't need to regenerate the
vector content.
All of these scenarios improve the responsiveness and interactivity of the application by optimizing the vector graphics.
• Do not overuse surfaces (movie clips with caching enabled). Each surface uses more memory than a regular movie clip, which
means that you should only enable surfaces when you need to improve rendering performance.
• Do not overuse bitmap caching. A cached bitmap can use significantly more memory than a regular movie clip instance. For
example, if the movie clip on Stage is 250 pixels x 250 pixels in size, when cached it might use 250K instead of 1K when it's a
regular (uncached) movie clip instance.
• Avoid zooming into cached surfaces. If you overuse bitmap caching, a large amount of memory is consumed (see previous bullet),
especially if you zoom in on the content.
• Use surfaces for movie clip instances that are largely static (nonanimating). You can drag or move the instance, but the
contents of the instance should not animate or change a lot. For example, if you rotate or transform an instance, the instance
changes between the surface and vector data, which is difficult to process and negatively affects your SWF file.
• Avoid mixing surfaces with vector data. If you mix surfaces with vector data, it increases the amount of processing that Flash
Player (and sometimes the computer) needs to do. Group surfaces together as much as possible; for example, when you create
windowing applications.
Working with Components in Flash Player
The component framework enables you to add functionality to components, but it can potentially add considerable file size to an
application. Components inherit from each other. One component adds size to your Flash document, but subsequent components that use
the same framework do not necessarily add more size. As you add components to the Stage, the file size increases, but at some point, it
levels off because components share classes and do not load new copies of those classes.
If you use multiple components that do not share the same framework, they might add substantial file size to the SWF file. For example, the
XMLConnector component adds 17K to the SWF file, and TextInput components add 24K to your document. If you add the ComboBox
component, it adds 28K, because it is not part of either framework. Because the XMLConnector component uses data binding, the classes
add 6K to the SWF file. A document that uses all these components has 77K before you add anything else to the file. Therefore, it is a good
idea to carefully consider your SWF file size when you add a new component to the document.
Components must exist in the parent SWF file's library. For example, a screen-based application must have a copy of the components it
uses in its library, even if those components are required by child SWF files that are loaded at runtime. This is necessary to ensure that the
components function properly, and slightly increases the download time of the parent SWF file. However, the parent library isn't inherited
or shared in the SWF files that you load into the parent. Each child SWF file must download to the application with its own copy of the same
components.
When you are planning to publish a SWF file with backward compatibility, you must have a good understanding of which components
have that capability. For information about component availability in different versions of Flash Player, see Table 3.
To enhance performance, you can change styles before they are loaded, calculated, and applied to the objects in your SWF file. If you can
change styles before the styles are loaded and calculated, you do not have to call setStyle.
The recommended practice to improve performance when using styles is to set properties on each object as objects are instantiated.
When you dynamically attach instances to the Stage, set properties in initObj in the call that you make to createClassObject(), as
the following ActionScript shows:
If you must restyle your components, you can improve efficiency in your application by using the Loader component. If you want to
implement several styles in different components, you can place each component in its own SWF file. If you change styles on the Loader
component and reload the SWF file, the components in the SWF file are recreated. When the component is recreated, the cache of styles is
emptied, and the style for the component is reset and referenced again.
Note: If you want to apply a single style to all instances of a component in your SWF file, change the style globally using
_global.styles.ComponentName.
You can use Unicode escape sequences to display special characters in Flash Player 6. However, it is possible that not all your characters
display correctly if you do not load text that is UTF-8 or UTF-16 encoded (Unicode) or if you do not use a Unicode escape sequence to
display the special character. Table 4 shows a list of commonly used escape sequences. For a set of Unicode code charts, see the
Unicode Character Code Charts By Script website.
Using System.useCodepage in your code forces the SWF file to use the system's code page instead of Unicode. Only use this process in
the following situations: when you are loading non-Unicode encoded text from an external location and when this text is encoded with the
same code page as the user's computer. If both these conditions are true, the text appears without a problem. If both of these conditions
are not true, use Unicode and a Unicode escape sequence to format your text. To use an escape sequence, add the following ActionScript
on Frame 1 of the Timeline:
this.createTextField("myText_txt", 99, 10, 10, 200, 25); myText_txt.text = "this is my text, \u00A9 2004";
This ActionScript creates a text field, and enters text that includes a copyright symbol (©). You can make a SWF file use the operating
system's code page, which is controlled by the useCodepage property. When Flash exports a SWF file, it defaults to exporting Unicode text
and System.useCodepage is set to false. You might encounter problems displaying special text, or text on international systems where
using the system's code page can seem to solve the problem of text incorrectly displaying. However, using System.useCodePage is
always a last resort. Place the following line of code on Frame 1 of the Timeline:
System.useCodepage = true;
Caution: The special character displays only if the user's computer has the character included in the font that is being used. If you are not
sure, embed the character or font in the SWF file.