actionscript-3 - Actionscript 3 加载外部 swf 转换问题

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

我在尝试在 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/

相关文章:

apache-flex - 以编程方式更改 Flex 标签/文本组件的字体、大小、粗细和颜色

java - 生成相关数字

actionscript-3 - AS3 : removing a lineStyle in the middle of drawing a shape

flash - 为什么我不能在 Flash CS4 的回调方法中访问组合框?

ios - NSParagraphStyle 属性名称 : Conversion from swift 3 to swift 4. 2

actionscript-3 - 使用as3创建正弦波音频-上下扫频

flash - Adobe Flash CS4 的文档?

flash - AS3 Sound.extract() 需要帮助

go - 将 JSON 解码为结构 - 列表中的列表类型?

python - 替换 Pandas 中的列值