gpt4 book ai didi

actionscript-3 - 评估包含 AS3 中嵌套影片剪辑的路径字符串

转载 作者:行者123 更新时间:2023-12-04 06:23:50 25 4
gpt4 key购买 nike

这应该相当简单,但我明白为什么它不起作用。我希望有一种聪明的方法可以做到以下几点:

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

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

现在要正常评估字符串,我会看起来像:

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

因为clip2 是movieclip 的 child 。

但我想用点语法解析或评估字符串以将字符串作为内部路径读取。
this.container[evaluatedpath];  // which is - this.container.movieclip.movieclip2

是否有能够将该字符串评估为内部路径的函数或技术?

谢谢。

最佳答案

据我所知,没有办法通过类似路径的参数来遍历 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;
}

关于actionscript-3 - 评估包含 AS3 中嵌套影片剪辑的路径字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com