.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
This entry was posted on Wednesday, December 23rd, 2009 at 5:45 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
December 24th, 2009 at 5:20 am
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?
December 24th, 2009 at 2:39 pm
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!
February 1st, 2010 at 11:31 am
[...] 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 [...]
April 4th, 2011 at 10:36 pm
[...] 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 [...]
September 15th, 2012 at 6:30 am
We also have haxe implementions here: http://code.google.com/p/touchmypixel/source/browse/#svn%2Ftrunk%2Fhaxe%2Fsrc%2Ftouchmypixel%2Fbitmap