使用 SWC - getDefinitionByName 问题 [英] Working with SWCs - getDefinitionByName issue

查看:24
本文介绍了使用 SWC - getDefinitionByName 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 fla 中有一堆图形资产,MovieClips 链接到一些类.我将 fla 导出为 swc ,然后使用合并到代码"选项将其添加到我的库中.

I have a bunch of graphics assets in a fla, the MovieClips are linked to some classes. I export the fla as a swc , which I add to my library with the option "Merged into Code".

一切都很好,我可以创建我的 MovieClips 的实例,只需像这样调用它们的类.

All works great, I can create instances of my MovieClips , just by calling their classes like this.

     //example 1
     var newMc:BaseClass = new GraphicAsset();

现在如果我想执行以下操作,Flash 会抛出错误,GraphicsAssetClass 为 null!

Now if I want do the following , Flash throws an error , GraphicsAssetClass is null!

    //example 2
    var GraphicsAssetClass:Class = getDefinitionByName("GraphicAsset") as Class;

我可以让上述行工作的唯一方法就是这样做

The only way I can get the above line to work is to do this

    //example 3
    var newMc:GraphicAsset;
    var GraphicsAssetClass:Class = getDefinitionByName("GraphicAsset") as Class;

    //then I'm able to do this 
    var newMc:BaseClass = new GraphicsAssetClass();

你能想出一个解决方案,我可以通过调用 getDefinitionByName() 来简单地获取类吗?就像我在示例 2 上所做的那样,而不必求助于示例 3 解决方案.

Can you think of a solution where I could simply get the class by calling getDefinitionByName() like I do on example 2 , without having to resort to example 3 solution.

推荐答案

问题是编译器会排除在代码中没有直接某处引用的任何类.这是编译器按设计应用的优化.

The problem is that the compiler will exclude any class that is not referenced directly somewhere in your code. This is an optimization that the compiler applies by design.

解决此问题的正常方法与您在示例 3"中显示的完全相同.但重要的是要注意,两条线不必在同一个地方在一起.只要

The normal way around this is exactly as you have shown in your "example 3". But it's important to note that the two lines don't have to be together in the same place. As long as

var newMc:GraphicAsset;

是从主执行路径引用的 SWF 中的某个位置,然后 GraphicAsset 的类定义将包含在最终的 swf 中.然后就可以调用了...

is somewhere in your SWF that is being referenced from the main path of execution, then the class definition for GraphicAsset will be included in the final swf. Then you can call...

var GraphicsAssetClass:Class = getDefinitionByName("GraphicAsset") as Class;

完全来自其他地方,它会按您的预期工作.

from somewhere else entirely, and it'll work as you expect.

甚至有可能(而且很常见)在单独的 SWF 中找到这两行.在这种情况下,一个 swf 会在运行时加载另一个 swf.通常,loader swf 是使用 getDefinitionByName("SomeClass) 的 swf,而 loaded swf 是定义 的 swfclass SomeClass,并使用 var a:SomeClass;,以确保将类内置到 swf 中.

It's even possible (and quite common) to find the two lines in seaprate SWFs. In this case, one swf would load the other swf at runtime. Typically, the loader swf would be the one using getDefinitionByName("SomeClass), and the loaded swf would be the one that defines class SomeClass, and uses var a:SomeClass;, to ensure that the class gets built into the swf.

出于这个原因,您经常会发现在库 swf"中定义了一个名为MyLibraryManifest"的类,然后从该 swf 的主要影片剪辑中引用.清单"类将看起来像这样:

For this reason, you often find a class called something like "MyLibraryManifest" defined in a "library swf", and then referenced from that swf's main movie clip. The "manifest" class then would simply look like this:

class MyLibraryManifest {

   private var a:GraphicAsset;
   private var b:SomeClass;
   private var c:SomeOtherClass;

   //... and so on...

}

<小时>

您可以使用 -includes-include-libraries 选项到 mxmlc 编译器,以强制链接器包含一个或多个单独的类(或整个 swc),而不应用我上面描述的修剪优化".


[edit] You can use the -includes or -include-libraries options to the mxmlc compiler, to force the linker to include one or more individual classes (or an entire swc), without applying the "pruning optimization" I described above.

查看 adobe 网站mxmlc 命令行选项的说明.

see adobe's site for explanation of the mxmlc command line options.

这些选项也可以在配置文件中指定,允许您控制 flex 和/或 Flash IDE 如何在后台调用编译器.

The options can also be specified in a config file, allowing you to control how flex and/or the Flash IDE will invoke the compiler behind the scenes.

祝你好运!

这篇关于使用 SWC - getDefinitionByName 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆