actionscript 3.0 で外部定義されたクラスを読み込もうとすると、何か問題が発生します。これは、ApplicationDomain / LoaderContext クラスの理解に問題があると思いますが、ドキュメントを読み、いくつかの Web 検索を行った後でも、まだ立ち往生しています。
基本的に、私がやりたいことは、呼び出し元の swf と読み込まれた swf の間で共有されるインターフェイスの実装であるシンボルを含む swf を読み込むことです。swf を正常にロードし、メソッドをインスタンス化して実行することはできますが、それを共有インターフェイス タイプにキャストしようとしない限りは問題ありません。キャストしようとすると、 TypeError: Error #1034: Type Coercion failed: type error が発生します。
これは、新しいクラスをロードすると、フラッシュが完全に異なるクラスとして認識されるため、例外が発生するためだと思われます。ドキュメントでは、ApplicationDomain を ApplicationDomain.currentDomain に設定して、LoaderContext 引数を使用することを提案しています。
問題は、これが効果がないことです。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::ExternalTestInterfaceImplementation@257b56a1 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();
}
}