Flash AS3 基于 XML 元素附加 SWC 符号
我有一个包含 8 个精灵的 SWC,每个精灵都有一个带有 Icon01、Icon02 等模式的链接标识符。我可以通过执行以下形式的操作来创建其中一个精灵的实例
var i:Icon01 = new Icon01();
this.addChild(i);
但是,我有一个 XML 文件,其中包含不同的消息,并且每条消息都包含一个以相同方式标记的图像元素,
...
<message>
<image>Icon01</image>
</message>
...
我希望能够解析 XML 并将相应的精灵附加到舞台上。这怎么能做到呢?
我的 XML 数据被解析为对象数组,XML 元素位于
var msgObj:Object = this.theMessages.messages.shift();
trace(msgObj.image);
但我不知道如何将其转换为精灵并将其添加到舞台。我尝试这样做
var s:Sprite = msgObj.image as Sprite;
this.addChild(s);
,但我只是收到有关 NULL 对象的错误。感谢您的任何帮助。
I have a SWC which contains 8 sprites, each of which has a linkage identifier with the pattern Icon01, Icon02, etc. I can create an instance of one of these sprites by doing something of the form
var i:Icon01 = new Icon01();
this.addChild(i);
However, I have an XML file which contains different messages, and each message contains an image element that is labeled in the same fashion
...
<message>
<image>Icon01</image>
</message>
...
I would like to be able to parse the XML and have the corresponding sprite attached to the stage. How can this be done?
My XML data is parsed into an array of objects and the XML element lives in
var msgObj:Object = this.theMessages.messages.shift();
trace(msgObj.image);
But I can't figure out how to cast it as a sprite and add it to the stage. I tried doing
var s:Sprite = msgObj.image as Sprite;
this.addChild(s);
And I just get an error about a NULL object. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getDefinitionByName(msgObj.image) 是可行的方法,但是您需要引用您可能在某处调用的每个类,否则它将无法编译。
在这种情况下,我所做的是创建一个永远不会被调用的虚拟函数,并列出其中的所有类,这样就可以强制编译器包含它。
另一种选择是使用
[embed]
功能加载它们,而不是使用 SWC,这样您就可以保证它们被编译并可以随时调用。getDefinitionByName(msgObj.image)
would be the way to go, but you will need to reference each class you might be calling somewhere otherwise it won't get compiled.what I do in this case is create a dummy function that never gets called and list all the classes there, that way you force the compiler to include it.
Another option is to load these using the
[embed]
feature instead of using the swc, that way you are guaranteed that they are compiled and can be called at any time.查看 flash.utils.getDefinitionByName () 或 loaderInfo。 applicationDomain.getDefinition(),如果资产已准备好/加载。
例如
HTH
Have a look at flash.utils.getDefinitionByName() or loaderInfo.applicationDomain.getDefinition(), if the assets are ready/loaded.
e.g.
HTH
在FlashDevelop 4.0.1(不确定旧版本)中,右键单击.swf 文件。 “添加到库”选项的正下方是“选项”。选择“包含的库(完全包含)”而不是“库(包含引用的类)”。
看起来,使用第一个选项时,当直接调用类(如 new BackgroundImage();)时,会引用该类,以便将其包含在内。当使用 var backgroundImage:Class = getDefinitionByName("BackgroundImage") as Class; 调用类时,它不会被直接引用,因此不会被包含在内。
通过选择第二个选项,.swc 中的所有类都会被包含在内,然后
getDefinitionByName()
就会起作用。In FlashDevelop 4.0.1 (not sure about older versions), right click on the .swf file. Right below the option to 'Add To Library' is 'Options'. Instead of 'Library (included referenced classes)' select 'Included Library (include completely)'.
It appears that with the first option, when calling the class directly like
new BackgroundImage();
the Class is referenced so it gets included. When the calling the class withvar backgroundImage:Class = getDefinitionByName("BackgroundImage") as Class;
it's not referenced directly so doesn't get included.By selecting the second option, all of the classes in the .swc get included and the
getDefinitionByName()
then works.