Saturday, November 5, 2016

optimization - Rendering performance for Flash games


I was reading on SO about native flash rendering vs building a custom BitmapData frame buffer and some of the answers were a bit conflicting, so I was wondering:



  1. Is it generally best practice to go the custom Bitmap buffer route or is it best to leave rendering to the flash engine?

  2. If you're using vector animations (MovieClips) as opposed to sprites, does that change the answer to the above?

  3. If so is it best practice to use sprite based animations?


(I'm targeting Flash 10 if that makes any difference)




Answer



1. There is no general best practice.


If you got a lot of (complex shaped) elements, particles etc. in your game, the bitmap buffer approach is going to be much faster. The bitmap buffer will also scale better with increasing complexity of your sprites. The vector renderer will become slower with more complex shapes or tween (shape tween) animations, it has some other benefits though:



  • You can freely scale your assets or even zoom into the scene without loss of quality.

  • Rotation or other transformations like skewing will be much easier to perform on vector graphics


So in conclusion, it boils down to what kind of game you're building.


2. Using MovieClips instead of bitmap sprite-sheets


I guess you meant "bitmap sprite-sheets" and not the AS3 Sprite class?



This doesn't make a difference, really. You can always turn your MovieClip based animations into animated bitmaps. Either manually, by exporting the movie to frames and turn them into a sprite-sheet, or dynamically, by rendering a sprite sheet from your MovieClip at runtime. Here's how I would do this:



  1. Create an instance of the MovieClip and stop() it.

  2. Create a new BitmapData object with the same width and height as the Movieclip.

  3. Use the draw() method, to render the MovieClip to the BitmapData.

  4. Store the BitmapData object in an Array or Vector.

  5. Go to the next frame of your MovieClip and repeat Steps 2 - 4, do this until you reached the last frame of your MovieClip. It's also a good idea to update two variables with the max width and max height of your MovieClip frames (since these can change from frame to frame).

  6. Now you can combine all the stored BitmapData Objects into a Sprite-Sheet (use BitmapData.copyPixels())


3. Sprite-based animations



As mentioned in the answer to your first question, there's no definite answer to that. If you need to perform a lot of transformations with your objects, eg. scaling and rotating you're probably better off by using the flash native renderer. If you got pre-defined animations that can be baked to sprite-sheets (manually or dynamically), and if you need to display hundreds of sprites at the same time, then go for a bitmap-rendering engine.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...