评估其中包含在AS3中嵌套影片剪辑路径字符串嵌套、字符串、路径、其中包含

由网友(执伞者)分享简介:这应该是相当简单,但我明白为什么它不工作。我希望有一个聪明的办法来做到以下几点:This should be fairly simple but I understand why it doesn't work. I am hoping there is a clever way to do the followin...

这应该是相当简单,但我明白为什么它不工作。我希望有一个聪明的办法来做到以下几点:

This should be fairly simple but I understand why it doesn't work. I am hoping there is a clever way to do the following:

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

I have a string 'movieclip1.movieclip2'

我有一个容器影片剪辑 - 集装箱

I have a container movieclip - Container.

现在评估该字符串通常我会是这个样子:

Now to evaluate the string normally I would look something like:

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

由于CLIP2是MovieClip子。

Because clip2 is a child of movieclip.

不过,我想分析或评估字符串与点语法阅读字符串作为内部路径。

But I would like to parse or evaluate the string with the dot syntax to read the string as a internal path.

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

是否有功能或技术,能够该字符串,以评估为一个内部路径?

Is there a function or technique to be able to evaluate that string into an internal path?

感谢。

推荐答案

据我所知,目前还没有办法去通过与路径状参数的图像显示,既不符合 [] getChildByName

As far as I know, there is no way to go through the DisplayList with a path-like argument, neither with [] nor getChildByName.

不过,您可以编写自己的函数来达到类似的效果(测试和工程):

However, you could write your own function to achieve a similar effect (tested and works):

/**
 * 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;
}
阅读全文

相关推荐

最新文章