评估其中包含在AS3中嵌套影片剪辑路径字符串 [英] Evaluate a path string which contains a nested movieclip in AS3

查看:209
本文介绍了评估其中包含在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 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;
}

这篇关于评估其中包含在AS3中嵌套影片剪辑路径字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆