首页 > 未分类 > 一个另类的swapChildren的实现
2019
12-06

一个另类的swapChildren的实现

一个另类的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);

------------------------------------------------------------------------------------------

原文出處: http://blog.csdn.net/tonywjd/archive/2008/01/11/2034737.aspx

在flex2中, 一個容器的子控制項相互重疊(如Canvas), 由z-order決定. swapChildren, swapChildrenAt用來交換兩個子控制項的z-order, 但有時會拋如下異常:

1can1.swapChildrenAt(1, 0);

RangeError: Error #2006: The supplied index is out of bounds.

1can1.swapChildren(clus1, t1);

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

我碰到這種異常的典型情況是當子控制項Resize到超出Canvas之外時. 不過可以用如下兩種方式替代實現控制z-order:

1). Put child1 at the most z-order:

1container.removeChild(child1);
2container.addChild(child1);

利用了每次添加的child總具有最大的z-order.

2). swap two children child1 and child2:

1var i1:int = container.getChildIndex(child1);
2var i2:int = container.getChildIndex(child2);
3container.setChildIndex(child1, i2);
4container.setChildIndex(child2, i1);

具體原因描述可在一個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

1<HBox>
2    <Button/>
3    <Button/>
4</HBox>

there are only two content children but, if the HBox has a background or
scrollbars, there can be additional non-children 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”

最后编辑:
作者:搬运工