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

2023-09-09 21:28:09 作者:萌獸

在这里根本的问题。通常,在AS3您在SWF通过加载器加载,而你得到的是假的MovieClip一些那种类型为装载机。

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".

有没有在阳光下的圣洁的方式来施放此加载的SWF到扩展影片剪辑,而不是装载机,假设SWF发布的自定义类型的基类的自定义类型?没有数据丢失?

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?

推荐答案

您可以做这样的事情:

code存根SWF:

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);
    	}
    }
}

我使用一个接口,但它不是严格neccesary。

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

package {

    public interface IStub {

    	function traceIt(value:String):void;

    }
}

code。在主SWF。

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");
}