繁体   English   中英

卸载SWF文件并在AS3中加载另一个SWF文件

[英]Unloading an swf file and loading another swf file in as3

我想卸载先前加载的swf文件(welcome.swf),并在用户单击位于第一个加载的swf文件内的按钮(即该按钮嵌套在欢迎SWF内部)时加载另一个swf文件(topics.swf)我已经尝试了所有方式的代码,但似乎都无法正常工作,而Im却无济于事。请有人协助。在此先感谢我。下面是我的主文件和外部SWF文件中的代码被卸载。

主文件

var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
box.addChild(loader);

外部SWF文件

var loader = new Loader();`

button_1.addEventListener(MouseEvent.CLICK, button4);

function button4(event:MouseEvent) {
    loader.unloadAndStop();
    loader=null;
    var SWFRequest:URLRequest = new URLRequest("Topics.swf");
    loader.load(SWFRequest);
    loader.x = 0;
    loader.y = 0; 
    addChild(loader);

}

我认为您应该在Main Loader上加载topic.swf。 因此,您应该从welcome.swf访问Main Loader。

这段代码行不通吗?

外部SWF文件

button_1.addEventListener(MouseEvent.CLICK, button4);

function button4(event:MouseEvent) {

    var target: Object = this;

    // Search the Main Loader from child swf.
    while (target.parent){
        target = target.parent;

        if (target is Loader){
            var loader:Loader = target as Loader;
            var SWFRequest:URLRequest = new URLRequest("Topics.swf");

            loader.load(SWFRequest);
            break;
        }       
    }
}

我可能会将所有加载/卸载职责移到主文件中,而内部swf只是通过loaderInfo.sharedEvents让主程序了解内部的某些事件,如下所示:

主文件:

var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);

function topicsRequestedHandler(event:Event):void {
    loader.contentLoaderInfo.sharedEvents.removeEventListener(
        "loadTopics", topicsRequestedHandler);
    loader.unloadAndStop();
    //load "Topics.swf" here
}   

loader.contentLoaderInfo.sharedEvents.addEventListener(
    "loadTopics", topicsRequestedHandler);

box.addChild(loader);

并在外部文件中:

button_1.addEventListener(MouseEvent.CLICK, button4);

function button4(event:MouseEvent) {
    loaderInfo.sharedEvents.dispatchEvent(new Event("loadTopics"));
}

(未经测试,但应该可以工作)

因此,对于所有已加载的文件使用此方案,您将把加载/卸载管理放在一个地方。

暂无
暂无

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

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