Loading swf and using it through interface

*爱你&永不变心* 提交于 2019-12-25 02:48:22

问题


I've created simple swf with interface:

public class Test extends MovieClip implements ITest
{

    public function Test()
    {
        Security.allowDomain("*");
        Security.allowInsecureDomain("*");
    }

    public function speak(str):String
    {
        trace(str);
        return "yeah";
    }
}

ITest:

public interface ITest {

    // Interface methods:
    function speak(str):String
}

And then I'm trying to load it:

    public function SWFLoader() 
    {
        var url='http://xxxxxxxx/test.swf';
        var loadURL:URLRequest=new URLRequest(url);
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
        var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
        loader.load(loadURL, context);
    }

    private function completeHandler(event:Event):void 
    { 
        var test:ITest;
        test = event.target.content as ITest;
        test.speak("ggg");
    } 

So if I have test.swf in the same directory(local way) it work's fine. But if I'm placing it on the dedicated server: (event.target.content as ITest) returns null. However, I can access speak() without interface like this event.target.content.speak("a!");

How to solve this problem?


回答1:


try this:

var test:ITest = ITest(event.target.content );

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html




回答2:


How do you share the ITest interface between your two swf ?

I imagine you have two projects one for the test.swf (the loaded one) and one for the loader (I'll call him loader.swf). I think you can't just declare the ITest interface twice (one for test.swf, one for loader.swf). If you do so, there will be two interfaces, with the same interface name, the same declared methods, but they still will be 2 different interfaces. And casting one into another will fail.
I bet that if you do (as suggested by PatrickS)

var test:ITest = ITest(event.target.content ); 

You will see a type error -> that's the advantage of this form of casting. This will confirm what I think : the two interfaces are different.

To really share the interface between your 2 projects, you should store it into a library (.swc file) and use that library in your 2 projects. This should solve the issue.



来源:https://stackoverflow.com/questions/3465612/loading-swf-and-using-it-through-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!