Flash 8: Projects for Learning Animation and Interactivity (OReilly Digital Studio)

10.1. Runtime Bitmap Caching

Many people, when hearing Flash 8's new compositing features described as bitmap rendering techniques, have a similar reaction: understandably, they think that these features are applicable only to imported JPEG, PNG, GIF, or other bitmap file formats. However, one of the best overall features of Flash 8 is its ability to treat movie clipsincluding vector-only movie clipslike bitmaps, for the purposes of compositing. This is a feature known as runtime bitmap caching.

You may be asking yourself why you would want to convert your beautifully sharp vectors into pixels. Fortunately, that's not exactly what's happening. It's true that the Flash Player will handle the movie clip as if it were a bitmap, but only when instructed to do so. Any vectors remain editable, and their appearance remains crisp and clean. Also, it's an automatic process that typically gives your file a significant performance boost.

Ideally, you should never have to think about what goes on behind the scenes in the Flash Player, but sometimes understanding a few basics helps you optimize your projects. The vector processing that makes Flash unique does not come without a price. Recalculating all of the vector math necessary to render shapes, colors, opacities, and more requires quite a bit of computer power. Moving pixels around the screen, however, is easier to achieve.

Accordingly, the Flash engineering team came up with a way for you to continue authoring with vectors as usual but, during runtime, have the player cache a bitmap version of your movie clip for compositing. For example, say your file contained a relatively complex vector-based movie clip that filled the Stage in layer 1, and additional complex movie clips that moved across this background. For every frame update, the Flash Player would have to factor in how all these vectors interacted, requiring a lot of energy.

But what if Flash took a temporary snapshot of these movie clips as it started rendering? In this case, the player would only have to move bitmaps across another bitmap, requiring a lot less work from your computer.

This simplified example demonstrates the principle behind runtime bitmap caching. Obviously, not all Flash projects will be as limited as the file described in this scenario. However, with this feature enabled, each time a movie clip experiences a major update, the bitmap will be re-cached and performance will again improve. Most movie clips will benefit from bitmap caching, and the cumulative effect can be substantial.

There are, of course, exceptions, the most notable of which are rotation and scaling. The interactions between movie clips and other elements in a Flash file require significant recalculating during rotation and scaling, so in these situations little boost will be experienced as a result of bitmap caching. However, when these operations conclude, performance will increase. Plus, the feature can be enabled and disabled with ActionScript, allowing you to use it when it is most suitable to do so.

An even bigger improvement can be seen when you tell Flash not to worry about transparencies within the movie clip. This is achieved by specifying an opaque background for any relevant clip. Clearly, not all clips can take advantage of this option, yet even when a movie clip contains areas of transparency (such as an irregular shape or an alpha value of less than 100%), this feature can be exploited temporarily. This feature, too, can be controlled with ActionScript. Therefore, you can give a movie clip an opaque background when it rests on a clear Stage, for example, but still see through the clip in situations when it is layered atop other elements.

10.1.1. Enabling Caching via the Interface and ActionScript

The runtime bitmap caching option of any movie clip can be enabled or disabled in the Properties panel with the click of a button. With a movie clip selected, you need only toggle the checkbox labeled "Use runtime bitmap caching," as seen in Figure 10-1.

Figure 10-1. Runtime bitmap caching can be enabled in the Properties panel with a movie clip selected

You can also do this using ActionScript. The syntax is as follows:

myClip_mc.cacheAsBitmap = true; myClip_mc.opaqueBackground = 0xFFFFFF;

The optional opaqueBackground property requires a color value for the solid background you wish to add. This sample code creates a white background for the movie clip.


Note: The value for the color is in hexadecimal notation, as used when specifying web colors. The syntax is very similar, but the typical number sign, or hash (#), is replaced by the prefix 0x. This is true when creating colors for many purposes in ActionScript.

Категории