一个另类的swapChildren的实现

本文介绍了一种在Flex中解决swapChildren方法异常的替代方案,通过移除并重新添加子元素来调整其Z-Order顺序,避免了在特定容器(如Canvas)中使用swapChildren和swapChildrenAt方法时遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个另类的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.youkuaiyun.com/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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值