使用字符串启动嵌入变量...变量不存在?
在我的动作脚本文件中,我有这个:
[Embed(source="assets/disk.png")]
protected static const Disk:Class;
如果我尝试创建一个具有以下内容的类:
var ClassReference:Class = getDefinitionByName("Disk") as Class;
我收到“磁盘”不存在的错误。有没有办法按名称启动 Disk 类,以便它使用字符串创建该类的新实例?
这有道理吗?
In my actionscript file I have this:
[Embed(source="assets/disk.png")]
protected static const Disk:Class;
if I try to create a class with the following:
var ClassReference:Class = getDefinitionByName("Disk") as Class;
I get an error that "Disk" does not exist. Is there a way to initiate the Disk class by name so it is creating a new instance of that by using a string?
Does that make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关闭,但您需要稍微调整一下,如下所示:
需要注意的是,这一行:
您应该用最原始的类型替换
DisplayObject
(我通常使用我自己的基类)。其他需要注意的事项:
使用
getDefinitionByName
时,您需要拥有完整的包路径。例如:如果遇到这样的错误:
您可能需要引用可以创建的类,如下所示:
Close, but you need to tweak it just a little, like this:
Something to note, this line:
You should replace
DisplayObject
with the most primitive type you can (I generally use my own base class).Other things to note:
You'll need to have the full package path when using
getDefinitionByName
. eg:If you run into errors like this:
You may need to make a reference to the classes that could be created like so:
只需使用
Disk
- 它是一个类,可以实例化:(它将是 ByteArrayAsset,因为 Embed 中未指定 mime 类型。)
Embed 会生成名称又长又晦涩的类,如果不解析 swf 就无法获取它,因此请使用 Disk const 代替。
更新:
据我了解,您想要嵌入资源并为其指定特定的类名称。 AFAIK,那是不可能的。您可以自己创建 Disk 类并将 Embed 放入其中:
然后您可以通过名称获取 Disk 类并使用 content 属性创建实例。
Just take the
Disk
- it's a class and can be instantiated:(it will be ByteArrayAsset because mime type is not specified in Embed.)
Embed will generate class with long obscure name, you can't get it without parsing swf, so use Disk const instead.
Update:
As I understand, you want to embed resource and give it specific class name. AFAIK, that's not possible. You can though create class Disk yourself and put Embed inside:
Then you can get Disk class by name and use content property to create instance.