关闭/卸载UiLoader as3

时间:2015-05-27 00:32:19

标签: actionscript-3 flash flash-cs6 uiloader

我使用Loader来显示SWF。我有一个加载SWF的按钮,我想要相同的按钮来关闭或隐藏或卸载SWF。

这是我的代码的一部分:

var so:Boolean = false ;
    glossary.addEventListener(MouseEvent.CLICK, glossaire)
    function glossaire (e:MouseEvent) {
        var myLoader:Loader = new Loader();                     
        var url:URLRequest = new URLRequest("glossary.swf");
        if (so == false )
        {
            so = true ;
            myLoader1.load(url);                                    
            addChild(myLoader);
        }
        else{  
            so = false ;
            //i tried  
            myUILoader.unload(); 
            removeChild(myUILoader);
            // but my loader still appear in the stage how can remove or hide it
        }
    }

2 个答案:

答案 0 :(得分:0)

您的问题是您的加载器是作用于click函数的,并且您每次单击都会创建一个新的Loader(当您真正只想在第一次单击时创建一个新加载器时)。此外,您似乎对您的加载器被调用感到困惑,因为您引用了myLoadermyLoader1myUILoader

//put the loader var out of the function scope here (so it persists after the function finishes). 
var myLoader:Loader;

glossary.addEventListener(MouseEvent.CLICK, glossaire);

function glossaire (e:MouseEvent){

    //instead of using the so boolean, just check if loader is null
    if (myLoader == null){
        myLoader = new Loader();
        myLoader.load(new URLRequest("glossary.swf"));                                    
        addChild(myLoader);
    }else{
        myLoader.unloadAndStop(); 
        removeChild(myLoader);
        myLoader = null; //set it to null now that it's been removed, so the next time this click function runs, a new loader will be made
    }
}

如果你只是想隐藏加载的swf(而不是完全卸载它),你可以这样做:

function glossaire (e:MouseEvent){
    //create the loader and load if it hasn't been done yet
    if (myLoader == null){
        myLoader = new Loader();
        myLoader.load(new URLRequest("glossary.swf"));                                    
    }

    //the parent property of the loader will be null if it hasn't been added (or has been removed) via addChild/removeChild
    if(myLoader.parent != null){
        removeChild(myLoader);
    }else{
        addChild(myLoader);
    }
}

答案 1 :(得分:0)

好的,让我告诉你解决这个问题的简单方法,

创建一个加载完成事件Event.Complete并将其侦听到一个函数,在那里设置一个布尔值,在你的情况var so:Boolean中使用相同的功能,

var so:Boolean = false ;
glossary.addEventListener(MouseEvent.CLICK, glossaire)
function glossaire (e:MouseEvent) {
    var myLoader:Loader = new Loader();                     
    var url:URLRequest = new URLRequest("glossary.swf");
    myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, swfload);
    if (so == false )
    {
        so = true ;
        myLoader.load(url);                                    
        addChild(myLoader);
    }
    else{  
        so = false ;
        //i tried  
        myUILoader.unload(); 
        removeChild(myUILoader);
        // but my loader still appear in the stage how can remove or hide it
    }
}

function swfload(e:Event):void
 {
   if(e.type == Complete)
   so = false;
 }