Tony Polinelli
Tarwin Stroh-Spijer

contact [at]
touchmypixel.com

6/25 Easey Street
Collingwood, 3066
Vic, Australia

+61 3 8060 5321

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/

5 comments


  1. I’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?


  2. We will be using the LGPL liscence for our code releases- you can do what you want with it :P
    I have a better version of the system with more useful features. Will upload a V2 soon. cheers

  3. TOdorus

    This 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=780658

    At 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.


  4. Spritesheets 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 :P


  5. Interestingly, 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. :/

 

Leave a Reply

Our Friends:

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