-1

I'm using a Loader to display a SWF. I have a button to load the SWF and I want that same button to close or hide or unload the SWF.

Here's part of my code:

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
        }
    }
4

2 回答 2

0

您的问题是您的加载器的范围仅限于点击功能,并且您每次点击都创建一个新的加载器(实际上您只想在第一次点击时创建一个新的加载器)。此外,您似乎对加载程序的名称感到困惑,因为您引用了myLoader,myLoader1myUILoader.

//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);
    }
}
于 2015-05-27T00:48:49.680 回答
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;
 }
于 2015-05-27T07:15:22.123 回答