據我所知,沒有辦法通過DisplayList以類似路徑的參數,[]
和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;
}
感謝。我會試一試。看起來像一個非常有用的功能。 – Ben 2011-06-07 05:04:14