AS3 Bitmap Cached Animations
Rendering a vector for every frame of a repeated animation (like in most games) take a lot of CPU. It takes even more CPU to have lots of complex characters on screen – many the same sprite.
cacheAsBitmap in flash is good, but only if you have static objects but recreates the bitmap data of an animation every frame, making it run even slower. The best solution is to cache each frame of an animation as a bitmapData, and then switch between them each frame. This works well until you have multiple instances of the same sprite – You dont want to pre-render the bitmap data and keeping it in memory many times, so it is best to keep a library of animations, and then reference the cached frames for each instance.
Another advantage of this technique is it seems to make use of Flash’s new multi-core support. On a dual core CPU it can, if you push it, take almost 100% of resources. Trying it on a quad-core seemed only to take up two CPUs though. I guess this is both a blessing and a curse as you’re more likely to grind a users system to a halt, even if they have multiple CPUs.
Animations copyright Nathan J. – http://www.scarygirl.com/
This entry was posted on Friday, April 25th, 2008 at 1:56 am and is filed under Flash, Games. 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.
5 comments
-
June 19th, 2008 at 5:39 amI’ve attempted similar things before, but simpler, and this is not a well-known technique. Your implementation is really well-done. What license is your code here under?
-
June 19th, 2008 at 11:35 amWe will be using the LGPL liscence for our code releases- you can do what you want with it

I have a better version of the system with more useful features. Will upload a V2 soon. cheers -
TOdorus
November 28th, 2008 at 10:36 amThis is not the best way to go about precaching. Initially my own prerender I used for precaching worked the same, but I had to make some new methods that can prerender and render spritesheets instead. The reason for this, is that the bitmapdata object is quite big compared to the actual grid it contains. In my test I found a ratio of about 100:4.6 in favor of spritesheets. I ran into memory problems because of this quite early on and switched to spritesheets instead.
More info here:
http://board.flashkit.com/board/showthread.php?t=780658At the moment the game I’ve initially build this prerender for exceeds the memory limit I used to hit (the total memory usage would be about 24mb before Flash crashed). This makes me think there’s some other limit in place having to do with bitmapdata’s. This is yet another reason to opt for spritesheets instead.
-
November 28th, 2008 at 10:57 amSpritesheets seems like a great improvement for memory usage. The game we’re working on at the moment requires about 50 frames for one character at 320xsomething – so pretty big. Damn boss characters. With the 2880 Bitmap data size this is a little restrictive – bring on flash 10 (no size limit).
Thanks alot for the heads up tho –
i could try it with bit-101′s ‘BigArseCanvas’ http://www.bit-101.com/blog/?p=1199 but that might kill performance a little – might get to some tests
-
November 29th, 2008 at 11:15 amInterestingly, all three rendering methods run at under 2 fps on my 1.0 Ghz PowerPC Mac in Safari, Flash Player 10. It’s much better in FireFox, though still not quite as fast as Flash 9. Huh. :/