简体   繁体   English

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

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

I want to unload a previously loaded swf file(welcome.swf) and load another swf file(topics.swf) when a user clicks a button that is located inside the first loaded swf file(ie the button, is nested inside the welcome swf file that is to be unloaded).I have tried all manner of codes but none seems to be working and Im at my wits end.Kindly someone assist.Thanks in advance.Below is the code in my main file and the external swf that is to be unloaded. 我想卸载先前加载的swf文件(welcome.swf),并在用户单击位于第一个加载的swf文件内的按钮(即该按钮嵌套在欢迎SWF内部)时加载另一个swf文件(topics.swf)我已经尝试了所有方式的代码,但似乎都无法正常工作,而Im却无济于事。请有人协助。在此先感谢我。下面是我的主文件和外部SWF文件中的代码被卸载。

Main file 主文件

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

External swf file 外部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);

}

I think you should load topics.swf at Main Loader. 我认为您应该在Main Loader上加载topic.swf。 So you should access Main Loader from welcome.swf. 因此,您应该从welcome.swf访问Main Loader。

Doesn't this code work? 这段代码行不通吗?

External swf file 外部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;
        }       
    }
}

I would probably move all the loading/unloading responsibility in a main file, with inner swf just letting the main know about some events inside through loaderInfo.sharedEvents just like this: 我可能会将所有加载/卸载职责移到主文件中,而内部swf只是通过loaderInfo.sharedEvents让主程序了解内部的某些事件,如下所示:

main file: 主文件:

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

and in external file: 并在外部文件中:

button_1.addEventListener(MouseEvent.CLICK, button4);

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

(not tested but should work) (未经测试,但应该可以工作)

So using this scheme for all the loaded files, you will keep loading/unloading management in one place. 因此,对于所有已加载的文件使用此方案,您将把加载/卸载管理放在一个地方。

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

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