Evaluate a path string which contains a nested movieclip in AS3

牧云@^-^@ 提交于 2019-12-11 04:13:21

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6248187/evaluate-a-path-string-which-contains-a-nested-movieclip-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!