我遇到了一些問題,試圖在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();
}
}
你能粘貼確切的錯誤信息? – 2010-08-27 00:10:38
當然。 我已經省略了行號,因爲導入等,但我可以肯定地說錯誤是在行 var castItem:TestInterface = TestInterface(obj); TypeError:錯誤#1034:類型強制失敗:無法將myPackage :: ExternalTestInterfaceImplementation @ 29b4e6a1轉換爲myPackage.TestInterface。 – user432437 2010-08-27 00:57:02