我在尝试在 actionscript 3.0 中加载外部定义的类时遇到了一些问题。我相信这是我对 ApplicationDomain/LoaderContext 类的理解的一个问题,但即使在浏览了文档和几个网络搜索之后,我仍然被卡住了。
本质上,我想要做的是加载一个包含符号的 swf,该符号是调用 swf 和加载的 swf 之间共享的接口(interface)的实现。我可以很好地加载 swf 并在其上实例化和执行方法,但前提是我不尝试将其强制转换为共享接口(interface)类型。如果我尝试强制转换它,我会收到 TypeError: Error #1034: Type Coercion failed: type error。
我怀疑这是因为当我加载新类时,闪存将其识别为完全不同的类,因此出现异常。 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);
}
}
接口(interface)声明:
public interface TestInterface
{
function getInterfaceId():String;
}
接口(interface)实现
public class ExternalTestInterfaceImplementation extends MovieClip implements TestInterface
{
public function getInterfaceId() : String
{
return "ExternalTestInterfaceImplementation!";
}
public override function toString():String
{
return getInterfaceId();
}
}
最佳答案
这似乎是由播放器中的错误引起的。我前段时间遇到过同样的问题。它在这里描述:
https://bugs.adobe.com/jira/browse/ASC-3529
有一个解决方法,基本上是用 URLLoader
加载 swf (作为二进制),然后使用 Loader::loadBytes
(而不仅仅是使用常规的 Loader
)。
这是此解决方法的说明和解决此问题的加载程序类:
http://blog.aleksandarandreev.com/?p=42
关于actionscript-3 - Actionscript 3 加载外部 swf 转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3580395/