AS3, loading in a SWF as a custom type

假如想象 提交于 2019-12-23 12:57:25

问题


Fundamental question here. Typically in AS3 you load in a SWF via the Loader, and what you get is some sort of pseudo MovieClip that is of type "Loader".

Is there any holy way under the sun to cast this loaded SWF to a custom type that extends MovieClip and not Loader, assuming the SWF was published with a base class of the custom type? Without data loss?

Alternatively, let's say you can't, can you even cast it from a custom type that extends Loader itself?


回答1:


You can do something like this:

Code in the stub swf:

package {

    import flash.display.MovieClip;

    public class Stub extends MovieClip implements IStub {

        public function Stub() {
            trace("Stub::ctor");
        }

        public  function traceIt(value:String):void {
            trace("Stub::traceIt " + value);
        }
    }
}

I'm using an interface, but it's not strictly neccesary.

package {

    public interface IStub {

        function traceIt(value:String):void;

    }
}

Code in the "main" swf.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT,handleInit);
loader.load(new URLRequest("Stub.swf"));

function handleInit(e:Event):void {
    var stub:Stub = loader.content as Stub;
//  or, using an interface 
//  var stub:IStub = loader.content as IStub;
    stub.traceIt("testing");
}



回答2:


According to comments on this article in the LiveDocs, you can cast Loader.content to MovieClip and access it that way.

However, there are some restrictions. For example, the SWF itself must be an AS3 SWF file, and the SWF doing the loading and the SWF being loaded should share the same sandbox.




回答3:


Yeah, loader.content gives you access to whatever was loaded. You SHOULD be able to simply cast that as whatever you want.

Alternately, you have the option to extend Loader, which already extends DisplayObjectContainer so you'll have most of the functionality of a MovieClip to begin with. In that case, write it so that you can simply call MyCustomClass.load(swf here) and it should do what you need it to.

I hope that helps!



来源:https://stackoverflow.com/questions/1554924/as3-loading-in-a-swf-as-a-custom-type

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