AS3 使用 getDefinition() 加载类
我在这方面遇到了障碍,如果有人能在这方面帮助我,我将不胜感激。我想做的是通过加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题在于以下代码行:
indexOf() 返回指定子字符串第一次出现的索引,如果子字符串不存在,则返回-1。因此,您将某个 int 的字符串表示形式(-1 或 0、1、2 等)传递给 getDefinition()...,这可能不会返回类引用。
假设您有一些名为 red1、red2、red3、red4 的剪辑,我会执行如下操作:
I think your problem lies in the following lines of code:
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: