Working with SWCs - getDefinitionByName issue

后端 未结 2 694
忘掉有多难
忘掉有多难 2021-01-12 07:06

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 Co

2条回答
  •  无人共我
    2021-01-12 07:17

    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.

    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;
    

    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.

    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.

    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...
    
    }
    

    [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.

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

    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.

    good luck!

提交回复
热议问题