【问题标题】:Evaluate a path string which contains a nested movieclip in AS3评估包含 AS3 中嵌套影片剪辑的路径字符串
【发布时间】:2011-09-09 01:02:42
【问题描述】:

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

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

我有一个容器影片剪辑 - Container。

现在要正常评估字符串,我会看起来像:

this.container['movieclip']['movieclip2']

因为clip2 是movieclip 的子级。

但我想用点语法解析或评估字符串以将字符串读取为内部路径。

this.container[evaluatedpath];  // which is - this.container.movieclip.movieclip2

是否有能够将该字符串评估为内部路径的函数或技术?

谢谢。

【问题讨论】:

    标签: actionscript-3 path nested movieclip evaluate


    【解决方案1】:

    据我所知,无论[] 还是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;
    }
    

    【讨论】:

    • 谢谢。我会试试看。看起来很实用的功能。
    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多