在主类中使用设计的接口转换加载的SWF

时间:2014-11-03 09:00:35

标签: actionscript-3 flash

首先,我阅读了相关主题,但不幸的是,任何提示都不适用于我:Casting a loaded SWF as an interface

我的问题确实是类似的,我已经创建了SWF(Content.swf),其中Content.as是那里的文档类:

package {   
import flash.display.MovieClip;

public class Content extends MovieClip implements IGame {

    public function igameMethod():void {
        trace("implemented method of IGame");
    }
}

}

IGame界面(与Content.as在同一目录中)包含唯一的方法' igameMethod'

package  {
public interface IGame {
    function igameMethod():void;
}

}

并且这样编译的SWF由LoadSWF中的Loader类加载,其中Main.as是文档类,它是这样的:

public class Main extends MovieClip {
    private var swfLdr:Loader;
    private var game:IGame;

    public function Main():void {
        if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        swfLdr = new Loader();
        var r:URLRequest = new URLRequest("../lib/Content.swf");
        var defaultLoaderContext:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);
        //none of settings for ApplicationDomain doesnt' work (with checkPolicy or with or without ApplicationDomain.currentDomain set)
        swfLdr.load(r,defaultLoaderContext);
        this.addChild(swfLdr);
        swfLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onExternalSWFLoaded);
        //tried Event.INIT too without success
    }

    private function onExternalSWFLoaded(e:Event) : void {
        var Game:Class = ApplicationDomain.currentDomain.getDefinition( "IGame" ) as Class;
        if (e.target.content is IGame) {
            trace("Is IGame");
        }else {
            //output of this condition is false 
            //so e.target.content is not IGame
            trace("No its not IGame");
        }
        var Game:Class = ApplicationDomain.currentDomain.getDefinition( "IGame" ) as Class; 
        //tried to cast as class without success too
        var gameContent: IGame = IGame(swfLdr.content); //app breaks here
       // TypeError: Error #1034: Type Coercion failed: cannot convert LoaderSWF@87f0089 to IGame
    }

我还使用与Main.as相同的包结构(与文档类位于同一文件夹中)的IGame interace(重复到LoadSWF项目),让LoadSWF在Content.swf上使用igameMethod(),但是我因为TypeError而无法使用它:错误#1034:类型强制失败:无法将LoaderSWF @ 87f0089转换为IGame。因此,编译器无法将此内容转换为IGame。

你有什么想法,这对我这个话题有帮助吗? 感谢您提前提示。 最亲切的问候帕维尔

0 个答案:

没有答案