简体   繁体   English

AS3,在SWF中加载为自定义类型

[英]AS3, loading in a SWF as a custom type

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". 通常在AS3中,您通过Loader加载SWF,而您获得的是某种类型为“Loader”的伪MovieClip。

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? 假设SWF是使用自定义类型的基类发布的,那么在阳光下是否有任何神圣的方式将此加载的SWF强制转换为扩展MovieClip而不是Loader的自定义类型? Without data loss? 没有数据丢失?

Alternatively, let's say you can't, can you even cast it from a custom type that extends Loader itself? 或者,假设你不能,你甚至可以从扩展Loader本身的自定义类型转换它吗?

You can do something like this: 你可以这样做:

Code in the stub swf: 存根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");
}

According to comments on this article in the LiveDocs , you can cast Loader.content to MovieClip and access it that way. 根据LiveDocs中本文的评论,您可以将Loader.content强制转换为MovieClip并以此方式访问它。

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. 例如,SWF本身必须是AS3 SWF文件,并且执行加载的SWF和正在加载的SWF应该共享相同的沙箱。

Yeah, loader.content gives you access to whatever was loaded. 是的,loader.content让您可以访问所加载的内容。 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. 或者,您可以选择扩展Loader,它已经扩展了DisplayObjectContainer,因此您可以开始使用MovieClip的大部分功能。 In that case, write it so that you can simply call MyCustomClass.load(swf here) and it should do what you need it to. 在这种情况下,写它,这样你就可以简单地调用MyCustomClass.load(swf here),它应该做你需要它。

I hope that helps! 我希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM