actionscript-3 - 评估 AS3 中包含嵌套影片剪辑的路径字符串

标签 actionscript-3 path nested movieclip evaluate

这应该相当简单,但我明白为什么它不起作用。我希望有一种聪明的方法可以做到以下几点:

我有一个字符串“movieclip1.movi​​eclip2”

我有一个容器影片剪辑 - 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/

相关文章:

css - 文本溢出省略号不适用于动态宽度

Flash CS4 + SQLITE

actionscript-3 - AS3 内部和自定义命名空间

flash - Flash中的透视

windows-7 - Windows7忽略部分路径(在我的例子中是powershell)

python - 通过两个 Pandas DataFrame 加速嵌套 for 循环

json - 使用 jq 将结构化 JSON 转为 CSV

javascript - 如何使用 Adob​​e flash 创建整页背景图像

testing - 如何使用 mocha 运行不同文件夹中的所有测试文件?

Python 2.6 文件存在 errno 17,