cacheAsBitmap
November 16th, 2006 . by polyGeekIf you have a SWF that has lots of vectors moving around and you notice that it’s bogging down a bit then you might want to think about using cacheAsBitmap. You can do this while you are authoring or at runtime.
See Flash help at: ActionScript classes > MovieClip > cacheAsBitmap (MovieClip.cacheAsBitmap property) for details.
Below is an example of how cacheAsBitmap can help make a Flash app run faster. You can toggle the cacheAsBitmap property off and on to see how it affects (or is that effects, I can never remember) the smoothness of the motion. I’ve tested on 3 PCs and noticed about a 20% increase in speed when the MovieClips are cached.
Here’s the complete code to look over with the explanation below - you can download files here.
First, take a look at the first two lines inside the All method. They give the arguments values just in case there were none passed. If your method can’t work without arguments and there are some logical defaults then by all means add the code to take care of that. It will save you a lot of time down the road.
If you’re unfamiliar with the ? : conditional then here’s a quick breakdown of how it works. It is essentially a very concise way of writing an if/else. What happens is that the expression preceding the “?” is evaluated as either true or false. If true then the first expression after the “?” is returned, otherwise the second.
If it’s still a little unclear try Flash help: ActionScript language elements > Operators > ?: conditional operator.
After that comes a for-in loop. This isn’t your typical loop in that it counts from one number to another. The for-in loop runs through all the dynamic properties in an object. In this case it runs though all the properties that belong to the passed argument - mc. Those properties could be lots of different things but usually they are MovieClips and that’s what we’re looking for. We want to apply the cacheAsBitmap property to each of the MovieClips. We also want to make sure that all of the MovieClips that might be in the passed argument - mc - are affected so we recursively call back to the method we are in - All - and send it the MovieClips that we have found.











