开发者

Evaluate a path string which contains a nested movieclip in 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:

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']

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?

Thanks.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜