2011-01-08 26 views
0

我有以下的代码组合:getChildAt用的addChild

seatContainer.getChildAt(order.seats[i]) 

我想要一个孩子加入到这一点,但它不容许我,我只能添加一个事件监听到这一点。

任何人都知道我可以添加一个孩子到这个没有使用eventListener?

回答

3

如果我没有记错,容器类(例如VBox,HBox等)中的getChildAt()返回DisplayObject。此对象类型不具有诸如“addChild”之类的方法 - 这些方法将在继承层次结构下面进一步介绍。

您需要将getChildAt()方法返回的引用强制转换为DisplayObject以外的其他东西;我相信你想要的方法是在DisplayObjectContainer中:

var child:DisplayObject = seatContainer.getChildAt(order.seats[i]); 
(child as DisplayObjectContainer).addChild(your_child_class_here); 
+0

这是正确的。如果您打算稍后使用小孩,那么您可能更愿意将getChildAt命令(作为DisplayObjectContainer)强制转换为var child:DisplayObjectContainer =(seatContainer.getChildAt(order.seats [i]); – Roy 2011-01-08 22:12:30