我遇到了一些问题,试图在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