2012-01-16 60 views
2

我試圖加載swc生成swf手動生成。由於我的特定環境,我們需要將類定義分離到swcs(有意義的地方),以便從輸出swfs中刪除多餘的代碼。Actionscript 3 - 腳本庫

簡而言之,我在用compc構建的swf中定義了一個類(LibA)。我將它編譯爲swc和目錄格式,以便我可以輕鬆地從目錄中提取library.swf以在運行時加載(外部鏈接),並使用swc從任何使用Flash CS5或mxmlc構建的swf編譯出來。

LibA.as

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

Main.as

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與

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

,我在構建Main(直接鏈接到舞臺的類)時,在Flash CS5的AS3設置中設置了適當的鏈接。

一切發佈都沒有問題。

然而,在運行時我得到VerifyError: Error #1014: Class LibA could not be found.

缺少什麼我在這裏?我希望能夠加載和使用我的Main.swf中的liba.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. 
+0

何時拋出錯誤,運行之後甚至在加載外部SWF之後? – turbosqel 2012-01-17 06:58:06

+0

在這個例子中,它引發了類的實例化。 – 2012-01-17 07:07:31

+0

因此,如果主應用程序無法訪問,則會將外部SWF加載到錯誤的應用程序域。你如何加載liba.swf,你使用LoaderContext? – turbosqel 2012-01-17 07:59:10

回答

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基本上與LibA的兩個定義(即被引用的和您正在加載的定義)相混淆 - 它們在2個不同的應用程序域中。

的解決方法是爲@turbosqel描述的那樣,與LoaderContext對象加載:

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類。

+0

你的假設是正確的,那就是做什麼(鏈接設置爲外部,我手動加載包含類定義的swf)。有趣的是,這在我使用的第三方運行庫中按預期工作,但不在Flash中。我的完整用例實際上更復雜一點,它引入了定義爲cross-swc的繼承(即'LibB'擴展'LibA')。 – 2012-01-17 00:48:05

+0

看到新的編輯 - 它在我的工作。另外,支持@ turbosqel – divillysausages 2012-01-17 10:12:06

+0

是的,它爲我修好了。如果@turbosqel沒有添加答案(因爲他確實先提出了修正),我會接受這個答案 - 感謝您的幫助。 – 2012-01-18 19:16:44

0

力霸不存在,直到您的SWF文件被加載,因爲你沒有將它導入到主。

在編譯時它不存在

移動var liba:LibA = new LibA();到上加載的SWF根函數調用和負載e.currentTarget.someFunc

+0

它實際上並沒有*在編譯時使用了swc定義。 – 2012-01-17 01:35:17

+0

如果您嘗試實例化類,則需要導入類。這正是你得到VerifyError的原因:錯誤#1014:無法找到類LibA。就像turbosqel建議使用LoaderContext一樣。 – 2012-01-17 16:07:52