I’ve read a lot of posts about how to get the best performance when rendering/drawing using Adobe AIR for mobile. Some claim using Sprites and setting cacheAsBitmap and cacheAsBitmapMatrix is the key. Others claim that drawing all graphics to a single Bitmap is the way to go.
Surprisingly, I haven’t read anything about the method I’ve often found to provide the best performance: using graphics.drawRect(). Yes, my tests on multiple handsets (including the Motorola Droid and HTC Evo) have suggested that I get a roughly 50% better frame rate when I use a single Shape object to draw all the objects/layers of my game.
There are several items to note about this approach:
- don’t use this approach for drawing static backgrounds that never change
- generate BitmapData for all graphics before starting the game/level
- creating or modifying BitmapData during the game loop is very, very expensive — try your best never to do this
- call ‘graphics.clear’ each frame before drawing to the Shape to avoid remnants from previous game loop cycles
- use ‘graphics.beginBitmapFill’ followed by ‘graphics.drawRect’ to draw on the Shape every frame
- minimize # of Shape objects — I usually can get away with one, but I’m sure there are some potential efficiencies to be gained from more complex systems with dirty flags
- use pools if you have to regularly instantiate any type of object — instantiation of objects can be very expensive & is often unnecessary
Using the Graphics object has been very successful for me but I want to say that I’m absolutely sure this is not the best solution in every scenario. Try each of the different approaches you’ve read about and see what works for you and your game. Another solution can be found here.