2011-06-06 20 views
1

这应该相当简单,但我明白为什么它不起作用。我希望有一个聪明的办法来做到以下几点:评估AS3中包含嵌套动画片段的路径字符串

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

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

我们评估串通常我会是这个样子:

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

因为CLIP2是MovieClip子。

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

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

是否有一种函数或技术能够评估该字符串到内部路径?

谢谢。

回答

2

据我所知,没有办法通过DisplayList以类似路径的参数,[]getChildByName

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

/** 
* 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

感谢。我会试一试。看起来像一个非常有用的功能。 – Ben 2011-06-07 05:04:14