这应该相当简单,但我明白为什么它不起作用。我希望有一种聪明的方法可以做到以下几点:
我有一个字符串“movieclip1.movieclip2”
我有一个容器影片剪辑 - Container。
现在要正常评估字符串,我会看起来像这样:
this.container['movieclip']['movieclip2']
因为clip2是movieclip的子元素。
但我想使用点语法解析或评估字符串,以将字符串读取为内部路径。
this.container[evaluatedpath]; // which is - this.container.movieclip.movieclip2
是否有函数或技术能够将该字符串计算为内部路径?
谢谢。
最佳答案
据我所知,无论是使用 []
还是 getChildByName
,都无法使用类似路径的参数来遍历 DisplayList。
但是,您可以编写自己的函数来实现类似的效果(经过测试并且有效):
/**
* 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;
}
关于actionscript-3 - 评估 AS3 中包含嵌套影片剪辑的路径字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248187/