Flex2 z-order问题解决

在flex2中, 一个容器的子控件相互重叠(如Canvas), 由z-order决定. swapChildren, swapChildrenAt用来交换两个子控件的z-order, 但有时会抛如下异常:

can1.swapChildrenAt(1, 0);
RangeError: Error #2006: The supplied index is out of bounds.

can1.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:
container.removeChild(child1);
container.addChild(child1);
利用了每次添加的child总具有最大的z-order.

2). swap two children child1 and child2:
var i1:int = container.getChildIndex(child1);
var i2:int = container.getChildIndex(child2);
container.setChildIndex(child1, i2);
container.setChildIndex(child2, i1);

具体原因描述可在一个mail list找到: (http://www.mail-archive.com/[email protected]/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
 

   

你可能感兴趣的:(RIA)