据我所知,没有办法通过 DisplayList 使用类似路径的参数,无论是 with[]
还是getChildByName
.
但是,您可以编写自己的函数来实现类似的效果(经过测试并且有效):
/**
* Demonstration
*/
public function Main() {
// returns 'movieclip2':
trace((container['movieclip']['movieclip2']).name);
// returns 'movieclip':
trace(path(container, "movieclip").name);
// returns 'movieclip2':
trace(path(container, "movieclip.movieclip2").name);
// returns 'movieclip2':
trace(path(container, "movieclip#movieclip2", "#").name);
// returns null:
trace(path(container, "movieclip.movieclipNotExisting"));
}
/**
* Returns a DisplayObject from a path, relative to a root container.
* Recursive function.
*
* @param root element, the path is relative to
* @param relativePath path, relative to the root element
* @param separator delimiter of the path
* @return last object in relativePath
*/
private function path(root:DisplayObjectContainer,
relativePath:String, separator:String = ".") : DisplayObject {
var parts:Array = relativePath.split(separator);
var child:DisplayObject = root.getChildByName(parts[0]);
if (parts.length > 1 && child is DisplayObjectContainer) {
parts.shift();
var nextPath:String = parts.join(separator);
var nextRoot:DisplayObjectContainer = child as DisplayObjectContainer;
return path(nextRoot, nextPath, separator);
}
return child;
}