RIAs, Adobe Flex, and related topics

About
Resume
Contact

June 3, 2007

How to swap your children

Filed under: Flex — jimrobson @ 10:44 pm

Have you run into issues when trying to swap your children? Rest assured - you are not alone.

The Flex 2 docs boast about how much easier depth management is in ActionScript 3. However, they don’t seem to mention that swapChildren() and swapChildrenAt() are buggy.

I was going crazy trying to manage the depth of some UI components till I discovered this Flexcoders post by Gordon Smith, which mentions the bug and a workaround.

The workaround suggested by Gordon, while somewhat klugy, does seem to work reliably. The basic idea is to use removeChild() and addChild() instead of swapChildren(). There is a gotcha here, though. You have to be careful to remove and add the children in the correct order, or you’ll be slapped with out-of-range errors at runtime. I put together a simple app to illustrate the bug and demonstrate the workaround. You can view the app here (right-click to download the source).

I’m feeling somewhat nostalgic for the days of Flash 8 and below, when you could put a movie clip at any depth that you felt like putting it. It didn’t matter whether or not there was anything at depth 9,999; you could still have your pop-up float around at 10,000. With the new display list approach, you no longer have that flexibility.

However, the ability to traverse the display list is a big advantage, and once Adobe gets all of the kinks worked out the new system will probably be far better than the old.

  • View sample here (when the app opens, right-click to view and download the source).

Powered by WordPress