An alternative implementation of swapchildren
I'm working on a project right now And swapchildren is just not working as expected.
Looked around a bit, and I just decided to remove the two children and add copies of them instead:
var tmpThis:Canvas= EV.target as Canvas;
var tmpHighest:Canvas= highestPanel;
removeChild (highestPanel);
removeChild (EV.target as Canvas);
addChild(tmpHighest);
addChild(tmpThis);
------------------------------------------------------------------------------------------
Original article: http://blog.csdn.net/tonywjd/archive/2008/01/11/2034737.aspx
In flex2, the sub-controls of a container are mutually duplicated (such as canvas), determined by Z-order. swapchildren and swapchildrenat are used to subscribe the Z-order of two sub-controllers. However, the following conditions may occur:
View SourcePrint?
1 |
can1.swapChildrenAt( 1 , 0 ); |
Rangeerror: Error #2006: The supplied index is out of bounds.
View SourcePrint?
1 |
can1.swapChildren(clus1, t1); |
Argumenterror: Error #2025: The supplied displayobject must be a child of the caller.
The typical scenario I have encountered is that when the sub-controller extends resize beyond the canvas, the following two methods can be used to replace the actual control Z-order:
1). Put Child1 at the most Z-order:
View SourcePrint?
1 |
container.removeChild(child1); |
2 |
container.addChild(child1); |
The maximum Z-order is always used for each added child.
2). Swap two children Child1 and child2:
View SourcePrint?
1 |
var i1: int = container.getChildIndex(child1); |
2 |
var i2: int = container.getChildIndex(child2); |
3 |
container.setChildIndex(child1, i2); |
4 |
container.setChildIndex(child2, i1); |
The cause description can be found in a mail list: (http://www.mail-archive.com/flexcoders@yahoogroups.com/msg61112.html ):
"You say your container is a displayobjectcontainer. Is it also a flex container
Such as canvas or vbox? If so, there is a bug with using swapchildrenat () and
Maybe with swapchildren () as well. Try using removechildat () and addchildat ()
Instead.
A flex container does tricky stuff with child indexes and overrides child
Management APIs such as numchildren, addchildat (), removechildat (), Etc .,
Because there are two kinds of children-content children and non-content
Children. If you write
View SourcePrint?
There are only two content children but, if the hbox has a background or
Scrollbars, there can be additional non-Children.
I think what happened is that the swapchildren () and swapchildrenat () Methods
Got added to displayobjectcontainer in Player 9, and the flex framework is not
Yet supporting them properly in the container class.
-Gordon"