A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Removing Loaded swf...

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    6

    Question Removing Loaded swf...

    Hi Im currently working on a flash website and am needing help with removing swf files that I import onto the main stage at the click of a button, So basically I want to unload the previous page before it loads the new page here is my code:

    Code:
    var mov1Request:URLRequest = new URLRequest("1.swf");
    var mov2Request:URLRequest = new URLRequest("2.swf");
    var mov3Request:URLRequest = new URLRequest("3.swf");
    var mov4Request:URLRequest = new URLRequest("4.swf");
    var mov5Request:URLRequest = new URLRequest("5.swf");
    var mov6Request:URLRequest = new URLRequest("6.swf");
    var mov7Request:URLRequest = new URLRequest("7.swf");
    var mov8Request:URLRequest = new URLRequest("8.swf");
    
    var mov1Loader:Loader = new Loader();
    var mov2Loader:Loader = new Loader();
    var mov3Loader:Loader = new Loader();
    var mov4Loader:Loader = new Loader();
    var mov5Loader:Loader = new Loader();
    var mov6Loader:Loader = new Loader();
    var mov7Loader:Loader = new Loader();
    var mov8Loader:Loader = new Loader();
    
    Button1.addEventListener(MouseEvent.CLICK, click1);
    Button2.addEventListener(MouseEvent.CLICK, click2);
    Button3.addEventListener(MouseEvent.CLICK, click3);
    Button4.addEventListener(MouseEvent.CLICK, click4);
    Button5.addEventListener(MouseEvent.CLICK, click5);
    Button6.addEventListener(MouseEvent.CLICK, click6);
    Button7.addEventListener(MouseEvent.CLICK, click7);
    Button8.addEventListener(MouseEvent.CLICK, click8);
    	
    mov1Loader.load(mov1Request)
    Background.addChild(mov1Loader);
    
    function click1(event:MouseEvent):void{
    	mov1Loader.load(mov1Request)
    	Background.addChild(mov1Loader);
    	}
    	
    function click2(event:MouseEvent):void{
    	mov2Loader.load(mov2Request)
    	Background.addChild(mov2Loader);
    	}
    	
    function click3(event:MouseEvent):void{
    	mov3Loader.load(mov3Request)
    	Background.addChild(mov1Loader);
    	}
    	
    function click4(event:MouseEvent):void{
    	mov4Loader.load(mov4Request)
    	Background.addChild(mov4Loader);
    	}
    	
    function click5(event:MouseEvent):void{
    	mov5Loader.load(mov5Request)
    	Background.addChild(mov5Loader);
    	}
    	
    function click6(event:MouseEvent):void{
    	mov6Loader.load(mov6Request)
    	Background.addChild(mov6Loader);
    	}
    	
    function click7(event:MouseEvent):void{
    	mov7Loader.load(mov7Request)
    	Background.addChild(mov7Loader);
    	}
    	
    function click8(event:MouseEvent):void{
    	mov8Loader.load(mov8Request)
    	Background.addChild(mov8Loader);
    	}
    Hope someone can help!

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    First off, you type in 6 times everything for each swf, you can , and you can make this considerably shorter (and easier to read) by using only one loader, and processing them one after the other, rather than all at once:
    PHP Code:
    var currSWF:uint 1;
    var 
    movLoader:Loader;
    var 
    swfs:Array = new Array();

    function 
    loadSWF():void
    {
       
    movLoader = new Loader();
       
    movLoader.load(new URLRequest(currSWF ".swf")); // will load in one SWF
       
    moveLoader.contentLoaderInfo.addEventListener(Event.COMPLETESWFComplete);

       
    currSWF++; // increace one time for the next round
    }

    function 
    SWFComplete(e:Event):void
    {
       var 
    swf:MovieClip e.currentTarget.content;
       
    swfs.push(swf); // storing them, not adding them -> easier later

       
    if(currSWF 8loadSWF(); // go for another round
       
    else
       {
          
    activateBtns(); // everything is loaded, time to activate the btns
          
    currSWF 0;
       }
    }

    function 
    activateBtns():void
    {
       var 
    currBtn:uint 1;
       
       for(var 
    i:uint 08i++)
       {
          
    this["Button" currBtn].addEventListener(MouseEvent.CLICKclickHandler);
          
    currBtn++;
       }
    }

    function 
    clickHandler(e:MouseEvent):void
    {
       
    // since your btns include a number in their name, we need the name
       
    var btnName:String String(e.currentTarget.name);
       
    // the number is the last character, but we'll need the uint version of it, not String
       
    var index:uint uint(btnName.charAt(btnName.length 1));

       if(
    index != currSWF// if the index doesn't match the currSWF variable
       
    {
          if(
    currSWF == 0addChild(swfs[index 1]); // there's no swf displaying, so just add it
          
    else 
          {
             
    removeChild(swfs[currSWF 1]); // remove the current one
             
    addChild(swfs[index 1]); // and add the new one
          
    }
       }
       
       
    currSWF index// this way we keep track of which swf is shown

    So that will make it tidier and normally it should remove the one showing before adding another one.
    Haven't tested it yet, so if you get any errors, please post them
    Last edited by florianvanthuyn; 08-11-2009 at 06:47 AM. Reason: changed some code
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    6
    ok so it loads with no errors but does nothing when hitting the buttons.
    I had for got to mention before that my SWF files acctualy have different names eg. main page, pictures, media etc.. and the actions are on the first frame of the first layer.
    Im not sure how this code will help with switching back and forth through pages.

  4. #4
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Ow, yeah, that will be a problem indeed.. I just wanted to clear a bit of lines for you
    But the way this should be solved will probably be something like I used.

    Use a variable to store the current swf. When clicking a button, check to see if the requested swf isn't already there, and if not, remove it by removing the reference variable (probably also of the type Loader in your case) and adding the new Loader (then set the reference variable equal to the new swf to continue the loop with the next click)
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  5. #5
    Junior Member
    Join Date
    Aug 2007
    Posts
    6
    Ok im still confused at how to solve this, AS2 was so much easier...could you give any example of code in how to do this?

  6. #6
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    For the different names you can use an Array:
    PHP Code:
    var currSWF:uint 1;
    var 
    movLoader:Loader;
    // so store the names in here, no .swf needed
    var swfNames:Array = ["firstName","secondDifferentName","otherTitle","someSWF"];
    var 
    swfs:Array = new Array();

    function 
    loadSWF():void
    {
       
    movLoader = new Loader();
       
    // we take one of the objects in the Array using the currSWF variable as index
       
    movLoader.load(new URLRequest(swfNames[currSWF] + ".swf")); // will load in one SWF
       
    moveLoader.contentLoaderInfo.addEventListener(Event.COMPLETESWFComplete);

       
    currSWF++; // increace one time for the next round
    }

    function 
    SWFComplete(e:Event):void
    {
       var 
    swf:MovieClip e.currentTarget.content;
       
    swfs.push(swf); // storing them, not adding them -> easier later

       
    if(currSWF 8loadSWF(); // go for another round
       
    else
       {
          
    activateBtns(); // everything is loaded, time to activate the btns
          
    currSWF 0;
       }

    Should work I guess

    Are your buttons on that same frame as where you put the code? Try putting a trace inside of clickHandler() I posted a while ago and comment out all the other code in there to see if they really aren't doing anything.

    EDIT: If your buttons have different names than Button0, Button1, etc than you'll have to use another Array as I used above for the buttons too!
    Last edited by florianvanthuyn; 08-22-2009 at 04:40 AM.
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center