我在尝试在actionscript 3.0中加载外部定义的类时遇到了一些问题。我认为这是我对ApplicationDomain / LoaderContext类的理解上的一个问题,但即使在浏览了文档和几次网络搜索之后,我仍然被卡住了。
实际上,我想要做的是加载一个swf,该swf包含一个符号,该符号是调用swf和加载的swf之间共享的接口的实现。我可以很好地加载swf并在其上实例化和执行方法,但前提是我不试图将其强制转换为共享接口类型。如果我真的尝试强制转换它,我会得到一个TypeError: error #1034:类型强制失败:类型错误。
我怀疑这是因为当我加载新类时,flash会将其识别为一个完全不同的类,因此出现了异常。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::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();
}
}发布于 2010-08-27 09:02:10
这似乎是由播放器中的错误引起的。不久前我也遇到过同样的问题。如下所示:
https://bugs.adobe.com/jira/browse/ASC-3529
有一个变通办法,基本上就是用一个URLLoader (作为二进制文件)加载swf,然后使用Loader::loadBytes (而不仅仅是使用一个常规的Loader)。
以下是对此解决方法的说明和解决此问题的加载器类:
http://blog.aleksandarandreev.com/?p=42
https://stackoverflow.com/questions/3580395
复制相似问题