您应该尝试拆分应该由本地可用名称组成的“路径”,并按顺序寻址每个对象。“本地可用名称”意味着应该有stage.LB
,并且该对象应该有一个属性anim
,等等。
function getObjectByPath(theRoot:DisplayObjectContainer,
thePath:String,separator:String='.'):DisplayObject
{
var current:DisplayObjectContainer=theRoot;
var splitPath:Array=thePath.split(separator);
while (splitPath.length>0) {
var named:DisplayObject = current.getChildByName(splitPath[0]);
var addressed:DisplayObject=current[splitPath[0]];
// either named or addressed should resolve! Otherwise panic
if (!addressed) addressed=named; else named=addressed;
if (!named) return null; // not found at some position
splitPath.shift();
if (splitPath.length==0) return named; // found, and last
current=named as DisplayObjectContainer;
if (!current) return null; // not a container in the middle of the list
}
// should never reach here, but if anything, just let em
return current;
}
这提供了两种解析路径的方法,按名称或按属性名称,并且属性名称优先。然后,您应该将结果类型转换为正确的类型。
是的,这样称呼它:
trace((getObjectByPath(stage,clockKeeper[key]) as TextField).text);