3

我遇到了一些問題,試圖在ActionScript 3.0中加載外部定義的類。我相信這對於我對ApplicationDomain/LoaderContext類的理解是一個問題,但即使通過文檔和一些網頁搜索後,我仍然陷入了困境。Actionscript 3加載外部swf鑄造問題

本質上我想要做的是加載一個包含符號的swf,該符號是調用swf和加載的swf之間共享的接口的實現。我可以很好地加載swf並實例化並執行方法,但只要我不嘗試將其轉換爲共享接口類型即可。如果我嘗試轉換它,我會得到一個TypeError:錯誤#1034:類型強制失敗:輸入錯誤。

我懷疑這是因爲當我加載新類時,閃存被認爲是完全不同的類,因此是例外。 documentation建議使用LoaderContext參數,並將applicationDomain設置爲ApplicationDomain.currentDomain。

問題是這個沒有效果。無論我將ApplicationDomain設置爲currentDomain,null還是當前域的子級,我仍然會得到類型強制失敗錯誤。該錯誤的::部分似乎表明,我加載的類是在不同的命名空間或一些這樣的,當我希望它是在我的加載程序相同的命名空間。

代碼:

import flash.display.Loader; 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.net.URLRequest; 
import flash.system.ApplicationDomain; 
import flash.system.LoaderContext; 

public class TestSkin extends MovieClip 
{ 
    var mLoader:Loader; 

    public function TestSkin() 
    { 
     super(); 
     startLoad("ExternalTest.swf"); 
    } 

    private function startLoad(url:String):void 
    { 
     mLoader = new Loader(); 
     var mRequest:URLRequest = new URLRequest(url); 
     var appDomain:ApplicationDomain = ApplicationDomain.currentDomain; 
        //Loading into different domains seems to have no effect 
     //var appDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); 
     //var appDomain:ApplicationDomain = null; 
     mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); 
     mLoader.load(mRequest, new LoaderContext(false, appDomain)); 
    } 

    private function onCompleteHandler(loadEvent:Event):void 
    { 
     //Get the object from the loadEvent 
     var obj:Object = loadEvent.target.content; 
     //Verify that the methods exist on the object 
     trace("Loaded item id: " + obj.getInterfaceId()); 
        //This returns Loaded item id: ExternalTestInterfaceImplementation! 
     //Try assigning as an instance of the shared type - fails with type coercion error 
        //Throws the following type error: 
        //TypeError: Error #1034: Type Coercion failed: cannot convert myPackage::[email protected] to myPackage.TestInterface. 
     var castItem:TestInterface = TestInterface(obj); 
     trace("castItem: " + castItem); 
    } 
} 

接口聲明:

public interface TestInterface 
{ 
    function getInterfaceId():String; 
} 

接口實現

public class ExternalTestInterfaceImplementation extends MovieClip implements TestInterface 
{ 
    public function getInterfaceId() : String 
    { 
     return "ExternalTestInterfaceImplementation!"; 
    } 

    public override function toString():String 
    { 
     return getInterfaceId();  
    } 
} 
+0

你能粘貼確切的錯誤信息? – 2010-08-27 00:10:38

+0

當然。 我已經省略了行號,因爲導入等,但我可以肯定地說錯誤是在行 var castItem:TestInterface = TestInterface(obj); TypeError:錯誤#1034:類型強制失敗:無法將myPackage :: ExternalTestInterfaceImplementation @ 29b4e6a1轉換爲myPackage.TestInterface。 – user432437 2010-08-27 00:57:02

回答

1

看起來這是由玩家的錯誤引起的。我前段時間遇到過這個問題。它在這裏描述:

https://bugs.adobe.com/jira/browse/ASC-3529

有一個變通方法,基本上包含在一個URLLoader加載SWF(二進制),然後使用(而不是僅僅使用普通LoaderLoader::loadBytes

下面是此解決辦法的explantion和修復此問題裝載機類:

http://blog.aleksandarandreev.com/?p=42

+0

謝謝,看起來像一個好地方開始。 – user432437 2010-08-27 01:07:04

+0

這絕對解決了這個問題,謝謝。 似乎有點奇怪,因爲臭蟲報告已經過去了一年,它仍然沒有被修復。我想知道是否有一些奇怪的東西沒有被傳達到安全上下文或什麼東西上,或者是一個真正的bug。 – user432437 2010-08-27 02:33:11