Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: Adobe Flex

Adobe Flex: Article

Cover Story: How to Increase the Frame Rates of Your Flash Movies

How's Your Need For Speed?

When are surfaces regenerated? Changes to a movie clip that make the player regenerate the internal bitmap representation should be used sparingly; otherwise you defeat the purpose of the feature. The bitmap that is generated when you turn bitmap caching on for a movie clip will be regenerated every time you change any of the following MovieClip ActionScript properties:

  • _xscale
  • _yscale
  • _rotation
  • _alpha
  • _width
  • _height
  • filters
  • blendMode
  • opaqueBackground
  • transform
So, try to avoid changing any of these ActionScript properties on a regular basis.

The bitmap will also be regenerated when:

  • The timeline play head 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 (i.e. The viewer Zooms in using the RightClick menu)
Memory Usage
Bitmap Caching naturally makes the Flash Player utilize 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, its bitmap representation is removed from memory.

You should be concerned about the amount of memory that your Flash Movie uses, because it can affect the performance of other applications that are running on the same computer as the Flash Player. The more memory the Flash Player uses, the less memory that is available for other programs to run effectively. Computers only have so much memory available to them; this comes in the form of RAM (Random Access Memory). Most computers nowadays have at least 256MB RAM. The operating system may provide more memory as and when required in the form of Virtual Memory. The Flash Player should never use that amount of memory, but now with the various new Bitmap related features added to the player, it is possible for Flash Movies to consume large amounts of memory in a short space of time so as the developer you should endeavor to minimize memory usage.

Before we talk about the specifics of bitmap caching memory usage, a little background information might be useful. Firstly, a bitmap is made up of pixels. It can be thought of as a grid of color values, which designate a particular color for each and every pixel. Each pixel is a cell in the grid. A bitmap that is 100 pixels wide, by 100 pixels high is described by a grid of 1000 color values, one for each pixel. Each color value in a bitmap is a binary number. A binary number is made up of bit's which can either be 0 or 1. This binary number will differ in length, depending upon the color depth of the bitmap. The color depth of a bitmap determines the range of possible color values that can be used in each pixel. For example, each pixel in a 24 bit image can be one of roughly 16.8 million colors. Those colors are formed by mixing together varying quantities of three primary colors, Red, Green and Blue. The three main colors are called Channels. Each channel can have 256 possible values (0255).

255 * 255 * 255 = 16.8 million
256 decimal is 11111111 in binary.

This binary number is 8 bits long. 8 bits is 1 byte. Therefore each channel in a color uses 1 byte. The bitmaps that are created by the Flash Player when it converts a movie clip into a surface have a 32 bit color depth. 32 bit images have four channels; Red, Green, Blue and an additional Alpha Channel. Therefore the color value for each pixel in a surface created by the Flash Player is 32 bits long, or 4 bytes.

4 * 8 = 32 bit

The bitmap that is created by the Flash Player to represent the visual state of a movie clip when you turn Bitmap Caching on will have the same dimensions (width and height) as the movie clip. A cached movie clip that is 100 pixels high and 100 pixels wide has 1000 pixels.

100 * 100 = 10,000 pixels

Each of those pixels will be 32 bits or 4 bytes. Therefore it will utilize a further 400,000 bytes of memory.

10,000 * 4 = 400,000 bytes

There are 1024 bytes in 1 kilobyte (KB). So, 400,000 bytes can also be said to be roughly 400 kilobytes.

Size Limits
Because of the possibility of this feature using excessive amounts of memory, Bitmap Caching will not work, if the dimensions of a cached movie clip are larger, or become larger, than 2880 pixels in either width or height. 2880 is a restriction Macromedia has put in place for all bitmaps in the player, to try and minimize excessive memory use. That is because a 2880 * 2880 movie clip that is cached will use up roughly 32MB of memory. Four of these cached movie clips with those same dimensions could potentially fill up a computer's memory and crash the machine.

Filters
It is worth noting here that if a filter effect is applied to a movie clip either in the Authoring Environment or using actionscript, then Bitmap Caching is automatically turned on and the cacheAsBitmap property will always return true, regardless of whether you turned Bitmap Caching off in the Authoring Environment or with ActionScript. To prove this, create a new movie clip, give it an instance name of scrollingMovieclip and add the following code to the first frame of the main timeline:

var blur=new flash.filters.BlurFilter(3,3,3) //guassian blur
scrollingMovieclip.filters=[blur]
trace(scrollingMovieclip.cacheAsBitmap) //outputs 'true'

When all of the filters effects are removed from a movie clip the cacheAsBitmap property will return to its previous state. So for example, if Bitmap Caching is turned off for a movie clip but it has filters applied to it, then removing all of those filters from the movie clip will turn Bitmap Caching off again:

scrollingMovieClip.filters=undefined
trace(scrollingMovieclip.cacheAsBitmap) //outputs 'false'

Loading External Content
Also when you load an external Flash Movie or image into a cached movie clip, using ActionScript, bitmap caching is turned off. This is because a movie clip is totally reset, all variables inside it are deleted, and all child movie clips are removed, all movie clip properties are set back to their default values.

To prove it, try the following code:

/*

This code fixes the onLoad bug, that is that anyMovieclip.onLoad event handler is deleted when loading external content into a movieclip

*/
_global.s_onLoad=function(f)
{
if(onLoadManager == undefined)
{
_global.onLoadManager={}
}
onLoadManager[this] =f
}
_global.g_onLoad=function()
{
return onLoadManager[this]
}
MovieClip.prototype.addProperty('onLoad', g_onLoad, s_onLoad)
this.createEmptyMovieClip("scrollingMovieclip",this.getNextHighestDepth())
scrollingMovieclip.onLoad=function()
{
trace(this.cacheAsBitmap)
//when the photo is loaded, show that bitmap caching was turned off
}
scrollingMoveclip.cacheAsBitmap=true
scrollingMovieclip.loadMovie("photo.jpg")

More Stories By Guy Watson

Guy Watson (aka FlashGuru) has been a well-recognized figure in the Flash community for around four years, supporting the community with tutorials and source files, moderating the large Flash community forums, and running his own Flash resource Web site - FlashGuru's MX 101. Guy was one of the two developers that created the award-winning zoom interface for Relevare and now works for Endemol UK, the creative force behind reality television, producing programs such as Big Brother and The Salon. Guy now spends most of his time developing Flash games and applications for high-profile clients such as Channel 5 Television, Ladbrookes, and UK Style.

Comments (24) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Conference Call 07/07/06 04:45:58 PM EDT

Trackback Added: Conference Call; Conference Call

Drug Rehab 07/07/06 09:19:22 AM EDT

Trackback Added: Drug Rehab; Drug Rehab

Payday Loan 07/05/06 10:59:50 AM EDT

Trackback Added: Payday Loan; Payday Loan

Dental Insurance 07/05/06 10:33:31 AM EDT

Trackback Added: Dental Insurance; Dental Insurance

Data Recovery 07/04/06 08:38:25 AM EDT

Trackback Added: Data Recovery; Data Recovery

Printers 07/02/06 12:24:47 PM EDT

Trackback Added: Printers; Printers

1031 Exchange 07/01/06 02:08:26 PM EDT

Trackback Added: 1031 Exchange; 1031 Exchange

First Aid Kits 07/01/06 03:49:12 AM EDT

Trackback Added: First Aid Kits; First Aid Kits

Car Accident Lawyer 06/30/06 02:25:19 PM EDT

Trackback Added: Car Accident Lawyer; Car Accident Lawyer

Acne 06/25/06 02:22:46 PM EDT

Trackback Added: Acne; Acne

Equity Line Of Credit 06/25/06 07:38:23 AM EDT

Trackback Added: Equity Line Of Credit; Equity Line Of Credit

Forklift 06/25/06 07:22:27 AM EDT

Trackback Added: Forklift; Forklift

Term Life Insurance 06/24/06 04:21:24 AM EDT

Trackback Added: Term Life Insurance; Term Life Insurance

Eye Surgery 06/23/06 06:58:00 PM EDT

Trackback Added: Eye Surgery; Eye Surgery

Relocation 06/23/06 06:35:57 PM EDT

Trackback Added: Relocation; Relocation

Payday Loan 06/23/06 06:33:04 PM EDT

Trackback Added: Payday Loan; Payday Loan

Payday Loan 06/23/06 03:15:54 PM EDT

Trackback Added: Payday Loan; Payday Loan

Relocation 06/23/06 03:10:34 PM EDT

Trackback Added: Relocation; Relocation

Distance Education 06/23/06 01:29:59 AM EDT

Trackback Added: Distance Education; Distance Education

Relocation 06/22/06 09:36:41 PM EDT

Trackback Added: Relocation; Relocation

Vasectomy 06/22/06 12:31:50 PM EDT

Trackback Added: Vasectomy; Vasectomy

Aeon 05/27/06 03:21:41 AM EDT

This tutorial is full of syntax errors.
Functions aren't terminated properly, statements aren't properly closed with semi-colons ;.
Needs amendment.

eddyC 02/19/06 07:05:15 PM EST

Hi "noname", I was searching for a solution to this problem and found out you've had the same tysRect-problem... Have you solved it by now? :-) TIA!

noname 01/05/06 06:00:11 PM EST

hey, i just took a shot at the example in "Cover Story: How to Increase the Frame Rates of Your Flash Movies": "Scrolling Large Amounts of Text".

Unless i'm doing something incredibly wrong, it's not close to complete the way you have it in the article. Anyway, i think i've narrowed it down and just need the value for one last property: "tysRect", in the scroll() function.

what is this supposed to be set to??? is there a working fla anywhere?

thanks!