Tony Polinelli
Tarwin Stroh-Spijer

contact [at]

6/25 Easey Street
Collingwood, 3066
Vic, Australia

+61 3 8060 5321

.net magazine (UK) Caching Animation in Flash/AS3

For the upcomming article that Tarwin wrote for .NET magazine on Caching Animations in Flash here is a quick rundown & downloadable example.

The code is split into two classes:


This is a very simple singleton class used to cache the rendered frames of any animation (MovieClip) that you want. All you need to do is call .cacheAnimation(identifier)  with the library identifier of your movieClip to store its frames in memory.


An animation displays the cached frames. You can retrieve instances from the AnimationCache via the getAnimation method. This will save memory, as each instance of the animation (if you have many jellyfish for example) will be referencing the same bitmap data. You can also create a new Animation(identifier), to create and cache a new animation from the library.

The animation class has similar functionality to MovieClip – play(), stop(), gotoAndPlay, etc.

To define the region that is cached (the first frame might be small, while frame 10 might be HUGE for example), you can define a ‘bounds’ clip. This is a child clip on the first frame of the MovieClip to be cached. It can also be used to crop an animation.

There are a few extra little features that we have in there that were used in scarygirl, you can find them out yourself ;P

Note: we have removed the queued caching system that we used in scarygirl, to simplify the code. If you are caching a lot of animations, you will need to look at ways to asyncronously cache frames. hint: think timeouts ;P


cheers Tony


  1. Zach

    Nice work, tony. I use a similar class for my animations in Away3D.
    One question about your implementation:
    You pass a string to the function and then look up the symbol. However, if you pass a string that does not have a symbol, and more specifically does not resolve to a movieclip, it will throw an error. (when it attempts gotoAndStop on a non movieclip object)
    So the question: why not make the function accept a movieclip? Its the only display object class that supports multiple frames. Is there an advantage to the approach I don’t see?

  2. Yeah, that would be a lot safer. I think it’s more how we used it generally, which was to just have a large array of strings (of the identifiers) set up for each level, then AFTER we load in the SWF asset file we’d then do the caching. If you’re going to use it in a system that is more tightly integrated it would probably be best to just throw it MovieClips I agree. I guess we should at least add in a try/catch. Will update after Christmas!

  3. [...] game ever” – thanks! We also wrote a tutorial for the magazine explaining how to use bitmap caching to speed up animations. The same month Scarygirl was also featured in the Australian Desktop [...]

  4. [...] I cached the dog’s shape tween animations as Bitmaps using TouchMyPixel’s AnimationCache. Each of these things had a small but helpful effect on the [...]

  5. Thanks AxG, good to see someone using it !


Leave a Reply

Our Friends:

Powered by haXe / poko cms | Copyright 2008-09 TouchMyPixel