-
http://twitter.com/chrisgannon Chris Gannon
-
Anonymous
-
http://twitter.com/chrisgannon Chris Gannon
-
http://twitter.com/chrisgannon Chris Gannon
-
Anonymous
-
Anonymous
-
Anonymous
-
http://twitter.com/chrisgannon Chris Gannon
-
LizGuest1
-
Anonymous
-
Anonymous
-
LizGuest1
-
Anonymous
-
LizGuest1
-
Anonymous
-
Chiken
-
Anonymous
-
http://www.facebook.com/greg.lindquist Greg Lindquist
-
vizio
-
Anonymous

Give me a MovieClip I’ll give you a sequence of Bitmaps!
Ok this is another of the problems that recurs over and over again while developing rich content flash applications: Vector Animation Optimisation.
Vector animation optimisation is a good friend when you have a lot of MovieClips visible on Stage that animate at the same time.
Lets jump to a concrete example.
Say you would like to have 500 robots like the following on the screen at the same time and all animating:
This is a very nice animation I found on the internet which I don’t know anything about. Like I don’t really now the structure of each frame, I don’t know how many MovieClips composite this animation, etc.
I’m mentioning this because a lot of the times, graphics are created to be easily animated, or easily expanded, and most of the time that approach clashes with optimisation. (Why? because you can end up with an infinite amount of nested MovieClips + Masks + complex strokes and fills)
I’m not criticising designers, because I think I would do the same if it was me doing that job: I always want to make my life easy!
So here is the result of having 500 instances of that MovieClip attached to the Stage and animating them at the same time (move the mouse pointer on top of the flash movie to start)
Notice that the target frame rate the swf was published to is 31 FPS.
You can see that the swf is running at about 1 FPS (At least on my machine, let me know if it’s different for you).
(cacheAsBitmap won’t help in this case because the cached bitmap will be invalidated every time the the next frame is loaded)
This is not good enough … BitmapData help us!!!
How to optimise this animation….
a) ask the designer to reduce the number of nested MovieClips
b) ask the designer to simplify strokes and fills
c) ask the designer to convert each frame into bitmaps and remove all the MovieClips
and these could be reasonable answers:
a) If I do that then it’s going to be a nightmare changing the animation
b) I don’t want to loose the level of details
c) Do I need to do this for each character? are you having a laugh? I hope you are going to give me a one click tool to do that!
so I don’t think those could be long term solutions.
And here is when BitmapData joins the circus.
What I have done is creating a function which from a given MovieClip it gives back a Vector of BitmapData objects. The Vector contains a number of items equal to the number of frames of the given MovieClip, and each Item is a bitmap snapshot of each frame.
Then I feed this Vector to another class which emulates the MovieClip class interface (e.g. gotoAndStop(frameNumber), play(), stop(), etc.),it copies the current frame’s bitmap into a Bitmap Object. I create 500 instances of this last class and attach them to the stage.
Here is the result (move the mouse pointer on top of the flash movie to start):
the frame rate stays at about 27 FPS, it’s not 31 FPS but it’s good enough.
this is a good improvement.
so what is the trick?
the trick is that I create exactly 500 Bitmap objects and only one copy of each frame of the entire animation is in memory, then each frame gets copied in each of the 500 MovieClips.
For the first example we have 500 instances of the Robot MovieClip on the stage. Each instance has its own timeline and Flash Player needs to render each frame for each instance.
While in the second case we create just one Vector that contains all the frames, which have been already rendered as BitmapData. Then we iterate through the Vector and we copy the current BitmapData in all the 500 Bitmap instances on the stage.
This works well when all the animations look the same, if you need 500 different animations you still get a goot frame rate but the memory usage will go up sharply. (You end up with a copy of each frame of each animation loaded in memory)
Another factor that can influence the frame rate is the size of the stage. If I take the swf above and publish it with a stage much bigger (say 1024×768) it will run at a lower frame rate even if we are using bitmaps. This is because Flash player needs to render a bigger space, taking longer.
Here you can find the class to convert a MovieClip into a Vector of BitmapDatas (MovieClipToBitmap.as) and also the class to control the Vector like a MovieClip (BitmapMovieClip.as).
Vizio