2011-06-06 28 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