2

我正在尝试手动加载生成的 swc 构建的 swf。由于我的特殊环境,我们需要将类定义分离到 swcs(这是有意义的)以从输出 swfs 中删除冗余代码。

简而言之,我LibA在构建的 swf 中定义了一个类 () compc。我将它编译成 swc 和目录格式,因此我可以轻松地从目录中提取 library.swf 以在运行时加载(外部链接)并使用 swc 从任何使用 Flash CS5 或mxmlc.

LibA.as

package
{
    public class LibA
    {
        public function LibA()
        {
            trace("*** LibA()");
        }
    }
}

主要作为

package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;

    public class Main extends Sprite
    {
        private var self:Main;
        private var context:LoaderContext;

        public function Main()
        {
            var l:Loader = new Loader();
            self = this;

            l.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event) {
                self.addChild(l.content);

                var liba:LibA = new LibA();
            });
            l.load(new URLRequest("./libs/build/liba.swf"));
        }
    }
}

我构建了 swc/目录 swc

compc -output libs/build/liba.swc -include-sources libs/LibA.as -debug=true

并且我在构建时在Flash CS5的AS3设置中设置了适当的链接Main(直接链接到舞台的类)。

一切都毫无问题地发布。

但是,在运行时我得到VerifyError: Error #1014: Class LibA could not be found.

我在这里想念什么?我希望能够加载和使用liba.swf从我的Main.swf.

完整的跟踪转储:

verify Function/<anonymous>()
                        stack:
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  0:getlex 4
                        stack: Main?
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  2:getlex 7
                        stack: Main? flash.display::Loader?
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  4:getproperty content
                        stack: Main? flash.display::DisplayObject?
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  6:callpropvoid addChild 1
                        stack:
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  9:findpropstrict LibA
                        stack: Object
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  11:constructprop 10 0
                        stack: *
                        scope: [global Object$ flash.events::EventDispatcher$ flash.display::DisplayObject$ flash.display::InteractiveObject$ flash.display::DisplayObjectContainer$ flash.display::Sprite$ Main$ Main Main] 
                         locals: Object flash.events::Event? * 
  14:coerce LibA
VerifyError: Error #1014: Class LibA could not be found.
4

2 回答 2

2

如果您想从 SWF 加载类,您需要执行类似的操作(在您的事件处理程序中):

var li:LoaderInfo = e.target as LoaderInfo; // get the loaderInfo object from the event
var swf:MovieClip = li.loader.content as MovieClip; // get the swf
var c:Class = swf.loaderInfo.applicationDomain.getDefinition( "LibA" ) as Class; // get the class definition for LibA

创建一个新的c应该给你你的LibA对象。您需要完整的类定义作为名称。

如果我了解您要执行的操作,我很确定您可以将 SWC 设置为嵌入外部库 - 也就是说您可以获得代码完成,但没有包含任何类,并且搜索了 SWC在运行时。

编辑

只是尝试了类似你正在做的事情。在我上面的示例中,当您创建时c,如果您跟踪它,它将跟踪LibA. 但是,如果您明确引用它,您将收到您描述的错误。我认为这是因为 Flash 与本质上的 2 个定义混淆了LibA- 被引用的一个和您正在加载的一个 - 它们位于 2 个不同的应用程序域中。

LoaderContext修复就像@turbosqel 描述的那样,用一个对象加载它:

var l:Loader = new Loader();
var context:LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain );
l.contentLoaderInfo.addEventListener(Event.COMPLETE, this._onLoadComplete );
l.load(new URLRequest("./libs/build/liba.swf"), context);

这对我有用,我现在可以明确引用LibA该类。

于 2012-01-17T00:25:28.397 回答
0

liba 在加载 SWF 之前不存在,因为您没有将它导入 main。

这意味着在编译时它不存在

移动var liba:LibA = new LibA();到加载的 swf 根上的函数调用并在加载后调用函数e.currentTarget.someFunc

于 2012-01-17T00:24:46.913 回答