September 09, 2003

Double-buffering in Flash

Today I was trying to figure out how to double-buffer in Flash. When you dynamically draw elements on the screen, and then need to redraw everything with new content, you would normally see everything clear, then the new elements appear.

Double-buffering is when you draw in the background while having the existing screen contents in place until the drawing is done. Then the newly drawn content is "popped to front" and the buffer cleared.

What I ended up doing was dynamically generating the contents of my screen as usual into my main movieclip called "screen." Then I rename the movieclip to "buffer" using movieclip._name (which is a writable property!), and swap the depth to place it on top. I then create a new empty "screen" movieclip behind it, and run my drawing routine again for my next screen. Once the new contents of "screen" are drawn, I remove the "buffer" movieclip. Since I am always drawing into the existing clip called "screen," I didn't have to rewrite my drawing code at all, or have to figure out some way to clone a movieclip with all of its children and their UI elements.

This worked really well. I have some problems knowing exactly when my drawing routine is finished, since loadMovie doesn't have a true onLoad() event, but it works fairly well.

It may not seem like rocket science to you, but I was overjoyed at how easy and effective this modification turned out to be.

Posted by andy at September 9, 2003 05:52 PM
Comments

I haven't used any of the DRK graphing components but I have been pondering real-time graphing. There are also several products out that, to my knowledge, do animated charts but that's only animating the static data in a clever way to a final position, not providing real-time (or near real-time) updating of constantly changing data. I may be wrong about the components/products since I haven't used them but it sounds like your technique would allow for creation of real-time charts.

Whatcha think?

(e)

Posted by: matte at September 9, 2003 08:08 PM

matte:

This technique applies to any form of dynamically-created elements on the screen. For example, slideshows, presentations, and user interfaces which are drawn or populated by programming code, instead of being positioned and displayed from the timeline.

As far as charts go, if you have a big project (and big bucks to go with it), I have used Corda's PopChart server with very good results. It generates charts on the fly in a variety of formats, including SWF. It can be interated with a database to produce a wide variety of reports.

It is pricey, though. Thousands of $US.

http://www.corda.com

Ironically, they weren't really showing off "dashboard" control panels on their site until we showed them our HotelTools Management Console that we built with Flash using their charting server. Shortly after, their "Decision Dashboard" appeared online. ;)

Posted by: andy makely at September 10, 2003 09:52 AM

Correct me if I wrong, but what you've done is not real double buffering. Real double buffering uses video
memory, so when the back buffer goes in front this happens instantly. The next vertical screen scanning, which hapens in 10ms max for 100 Hz mode, uses new buffer content at once. Flash never uses video memory for double buffering. When you remove the 'buffer' movieclip (in your example), flash still has to do lots of work before 'screen' content is really shown. I don't know whether flash renders invisible movieclips, but I wonder if it does, because this would highly impact overall performance. So, most likely, flash still needs to *render* all
the 'screen' content after 'buffer' is removed, even if all 'screen' movieclips properties were alredy set
while 'screen' was invisible. If flash does render invisible movieclips in (system) RAM, it still has to move
rendered data from RAM to video memory, although I gues this case is very unlikely. Anyway, the technique you
explained cannot give the same effect as real double buffering, it cannot even get close.

Posted by: max at March 2, 2004 12:53 PM

Of all the words of mice and men, the saddest are `It might have been.

Posted by: Mitra Kanchan at March 17, 2004 12:02 PM

I just ran into this issue today because of loadMovie. I guess all you can do is work around it or create some fake buffer effect, where you animate off stage and push it on stage when its called.

I've noticed moving stuff onto the stage wont make it blink, if all you have to do is change the X and Y axis.

Posted by: flash gallery at December 20, 2006 10:09 AM
Post a comment