【问题标题】:Actionscript 3 loading external swf casting issueActionscript 3 加载外部 swf 转换问题
【发布时间】:2011-04-04 13:40:22
【问题描述】:

我在尝试在 actionscript 3.0 中加载外部定义的类时遇到了问题。我相信这是我对 ApplicationDomain / LoaderContext 类的理解的一个问题,但即使在浏览了文档和一些网络搜索之后,我仍然卡住了。

基本上我想要做的是加载一个包含符号的 swf,该符号是调用 swf 和加载的 swf 之间共享的接口的实现。我可以很好地加载 swf 并在其上实例化和执行方法,但前提是我不尝试将其强制转换为共享接口类型。如果我尝试强制转换它,我会收到 TypeError: Error #1034: Type Coercion failed: type error。

我怀疑这是因为当我加载新类时,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();    
    }
}

【问题讨论】:

  • 你能粘贴确切的错误信息吗?
  • 当然。由于导入等原因,我省略了行号,但我可以肯定地说错误是在 var castItem:TestInterface = TestInterface(obj); TypeError:错误 #1034:类型强制失败:无法将 myPackage::ExternalTestInterfaceImplementation@29b4e6a1 转换为 myPackage.TestInterface。

标签: actionscript-3 flash-cs4 type-conversion loader applicationdomain


【解决方案1】:

这似乎是由播放器中的错​​误引起的。我前段时间遇到过同样的问题。这里有描述:

https://bugs.adobe.com/jira/browse/ASC-3529

有一个解决方法,基本上是使用URLLoader(作为二进制文件)加载swf,然后使用Loader::loadBytes(而不是仅仅使用常规的Loader)。

这里是对这个变通方法的解释和修复这个问题的加载器类:

http://blog.aleksandarandreev.com/?p=42

【讨论】:

  • 这绝对解决了问题,谢谢。自从报告该错误以来已经一年了,但仍未修复,这似乎有点奇怪。我想知道这是否是一些关于安全上下文或其他内容尚未传达的奇怪现象,还是真正的错误。
猜你喜欢
  • 2011-02-12
  • 2013-04-12
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2011-03-29
  • 2011-11-01
  • 2010-11-03
  • 2011-07-17
相关资源
最近更新 更多