AS3 使用 getDefinition() 加载类

发布于 2024-09-12 01:20:33 字数 1013 浏览 1 评论 0原文

我在这方面遇到了障碍,如果有人能在这方面帮助我,我将不胜感激。我想做的是通过加载 swf ('index.swf') 来使用共享运行时库,该 swf ('index.swf') 有许多按顺序命名的库对象,例如:

(orange1,orange2,orange3,orange4) (red1,red2,red3,red4)

我能够毫无问题地加载 swf('index.swf'),甚至能够加载正确的库资源,但我必须将全名声明为字符串,例如 getDefinition (“橙色1”)。我想做的是匹配字符串的前三个字母,然后运行 ​​for 循环来加载与前三个字母匹配的所有类。我通常可以通过使用indexOf()方法来做到这一点。

这是我的代码:

public function loadContent():void
{

ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onloadHandler);
ldr.load(req);

}

public function progressHandler(eProgress:ProgressEvent):void
{
var percent:Number = (eProgress.bytesLoaded / eProgress.bytesTotal);
trace(percent);
}

public function onloadHandler(e:Event):void
{
// THIS IS WHERE I AM TRYING TO MATCH THE STRING
var str:String = "red";
str = (str.indexOf(str));
var ref1:Class = e.currentTarget.applicationDomain.getDefinition(str) as Class
trace(ref1);

}

我非常感谢您的帮助。

谢谢。

I have hit a road block on this and would highly appreciate if someone can help me on this, please. What I am trying to do is to use shared runtime library by loading a swf ('index.swf') which has numerous library objects which are named in sequence such as:

(orange1,orange2,orange3,orange4)
(red1,red2,red3,red4)

I am able to load the swf('index.swf') without any issues and even am able to load the right library asset, but I have to declare the full name as string such as getDefinition('orange1'). What I would like to do is to match first three letters of string and then run a for loop to load up all the classes that match the first three letters. I usually can do this by employing indexOf() method.

here is my code:

public function loadContent():void
{

ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onloadHandler);
ldr.load(req);

}

public function progressHandler(eProgress:ProgressEvent):void
{
var percent:Number = (eProgress.bytesLoaded / eProgress.bytesTotal);
trace(percent);
}

public function onloadHandler(e:Event):void
{
// THIS IS WHERE I AM TRYING TO MATCH THE STRING
var str:String = "red";
str = (str.indexOf(str));
var ref1:Class = e.currentTarget.applicationDomain.getDefinition(str) as Class
trace(ref1);

}

I would highly appreciate your help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-09-19 01:20:33

我认为你的问题在于以下代码行:

str = (str.indexOf(str));
var ref1:Class = e.currentTarget.applicationDomain.getDefinition(str) as Class 

indexOf() 返回指定子字符串第一次出现的索引,如果子字符串不存在,则返回-1。因此,您将某个 int 的字符串表示形式(-1 或 0、1、2 等)传递给 getDefinition()...,这可能不会返回类引用。

假设您有一些名为 red1、red2、red3、red4 的剪辑,我会执行如下操作:

for (var i:int=0; i < 4; i++) {
     var classRef:Class = e.currentTarget.applicationDomain.getDefinition("red" + (i+1).toString()) as Class;
     trace(classRef);
}

I think your problem lies in the following lines of code:

str = (str.indexOf(str));
var ref1:Class = e.currentTarget.applicationDomain.getDefinition(str) as Class 

indexOf() returns the index of the first occurrence of the specified substring or -1 if the substring doesn't exist. So , you are passing a string representation of some int (either -1 or 0, 1, 2, etc) to getDefinition()... which probably isn't returning a class reference.

Assuming you have some clips named red1, red2, red3, red4 I would do something like the following:

for (var i:int=0; i < 4; i++) {
     var classRef:Class = e.currentTarget.applicationDomain.getDefinition("red" + (i+1).toString()) as Class;
     trace(classRef);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文