在舞台上获取TextArea(通过ID,名称获取的getChild)
问题描述:
我试图在FlashBuilder的舞台上获取7个TextAreas,它们全都具有“Desc1”,“Desc2”,“Desc3”.. 。和名称相同的“DESC1”,“DESC2”,“Desc3” ......,但是当我设法得到它,我得到一个空对象的错误...在舞台上获取TextArea(通过ID,名称获取的getChild)
for(var i:int = 0;i<7;i++)
{
trace((stage.getChildByName("Desc"+(i+1))as TextArea).x);
}
我搜索该网站并没有找到任何“getChildByID”的方法
答
Flex ID不适用于getChildByName()。 getChildByName()被设计为与Adobe Flash CS中的嵌套元素的ID一起使用。
flex id是一个类名称与id相等的类成员的显式声明。 由于actionscript语言中缺少宏,因此无法自动创建此类控件列表。
您可以手动创建一个Vector或文本区域的阵列,并用它在你的代码的其他部分,在你的文字区域自动重复:
var text_areas:Vector.<TextArea> = new Vector.<TextArea>();
text_areas.push(Desc1, Desc2, Desc3);
// or you can do this
var text_areas2:Array = [];
text_areas["Desc1"] = Desc1;
text_areas["Desc2"] = Desc2;
text_areas["Desc3"] = Desc3;
// Now you can iterate over the text areas
for each (var a_text_area:TextArea in text_areas)
{
....
}
或者你可以创建一个柔性阵列:
<fx:Array id="textAreas">
<s:TextArea id="textArea1"/>
<s:TextArea id="textArea2" x="397" y="0"/>
<s:TextArea id="textArea3" x="201" y="1"/>
</fx:Array>
追踪“stage.numChildren” - 你看到了多少物品?赔率是,你的TextAreas不是你舞台的直接子女,而是嵌套在另一个对象中。 getChildByName不会执行对舞台下所有儿童的深层搜索。 – meddlingwithfire