A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: how to apply removeChild to multiple .swf's

  1. #1
    Senior Member
    Join Date
    Apr 2003
    Location
    Seattle
    Posts
    177

    how to apply removeChild to multiple .swf's

    Hello,

    I have a project in which the user has the option of pressing any one of 5 keyboard keys that each loads a different .swf file. I would like whatever key the user presses to unload whatever swf is currently playing and load whichever one is associated with the key pressed.

    I have no problem coding this with two .swf's --each one loads and simultaneously unloads the other-- however when I add a third, it becomes erratic and the third removeChild function does not work properly.

    It would seem that the problem is that only one removeChild can be applied at a time. Is there a way around this?

    The code I am using is on a frame within the main timeline and it is not associated with a container other than the stage onto which the the swf's are directly loaded:

    Code:
    //variables to call swf's
    var myLoader:Loader = new Loader();
    var myRequest:URLRequest=new URLRequest("swf/darker.swf");
    
    var Loader2:Loader = new Loader();
    var Request2:URLRequest=new URLRequest("swf/animalinside.swf");
    
    var Loader3:Loader = new Loader();
    var Request3:URLRequest=new URLRequest("swf/berlin1.swf");
    
    //darker = myLoader : press keyboard #3 to load
    stage.addEventListener(KeyboardEvent.KEY_DOWN, Darker);
    function Darker(e:KeyboardEvent):void{
    
    if (e.charCode == 51){ //51=3
    myLoader.load(myRequest); 
    addChild(myLoader); 
    removeChild(Loader2);
    removeChild(Loader3);
    }
    }
    
    //animalInside = Loader2 : press keyboard #4 to load
    stage.addEventListener(KeyboardEvent.KEY_DOWN, animalInside);
    function animalInside(e:KeyboardEvent):void{
    
    if (e.charCode == 52){ //52=4
    Loader2.load(Request2); 
    addChild(Loader2); 
    removeChild(myLoader);
    removeChild(Loader3);
    }
    }
    
    //berlin1 = Loader3 : press keyboard #5 to load
    stage.addEventListener(KeyboardEvent.KEY_DOWN, Berlin1);
    function Berlin1(e:KeyboardEvent):void{
    
    if (e.charCode == 53){ //53=5
    Loader3.load(Request3);
    addChild(Loader3);  
    removeChild(myLoader);
    removeChild(Loader2);
    }
    }
    I have tried adding multiple instances within the removeChild() parens like this: removeChild(Loader3, Loader2, Loader4, etc.); however it doesn't work.

    Stacking the removeChild also doesn't seem to work:
    removeChild(myLoader);
    removeChild(Loader2);
    removeChild(Loader3);
    etc.

    Not sure how to proceed, any help would be much appreciated!

    Thanks in advance,

    Llyfre

  2. #2
    Senior Member
    Join Date
    Jan 2010
    Posts
    141
    Why use 3 different loaders? You can just use one loader and then use the unload (or even unloadAndStop) properties and then load the new swf. You can also combine all 3 functions into one function.

    Code:
    var myLoader:Loader = new Loader();
    
    //I just picked a random swf to have loaded first. If you don't want a swf loaded at first remove just the first line. 
    myLoader.load("swf/darker.swf"); 
    addChild(myLoader);
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, Darker);
    function Darker(e:KeyboardEvent):void{
    //unload what is in loader
    myLoader.unload();
    
    if (e.charCode == 51){ //51=3
    myLoader.load("swf/darker.swf");
    }
    
    if (e.charCode == 52){ //52=4
    myLoader.load("swf/animalinside.swf");
    }
    
    if (e.charCode == 53){ //53=5
    myLoader.load("swf/berlin1.swf");
    }
    
    }

  3. #3
    Senior Member
    Join Date
    Apr 2003
    Location
    Seattle
    Posts
    177

    Resolved!

    Didn't know I could use just one loader --thanks for the elegant solution!

    Now I have the perfect controls to which I simply added a line to stop the sounds in order that when the loader unloads the associated sounds cut off too.

    As a reference for others who might read this, here is the full code that I ended with:

    Code:
    stop();
    
    var myLoader:Loader = new Loader();
    var myRequest:URLRequest=new URLRequest("swf/darker.swf");
    var Request2:URLRequest=new URLRequest("swf/animalinside.swf");
    var Request3:URLRequest=new URLRequest("swf/berlin1.swf");
    var Request4:URLRequest=new URLRequest("swf/berlin2.swf");
    var Request5:URLRequest=new URLRequest("swf/time.swf");
    
    addChild(myLoader);
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, externals);
    function externals(e:KeyboardEvent):void{
    myLoader.unload();
    SoundMixer.stopAll();
    
    if (e.charCode == 51){ //51=3
    myLoader.load(myRequest);
    }
    
    if (e.charCode == 52){ //52=4
    myLoader.load(Request2);
    }
    
    if (e.charCode == 53){ //53=5
    myLoader.load(Request3);
    }
    
    if (e.charCode == 54){ //54=6
    myLoader.load(Request4);
    }
    
    if (e.charCode == 55){ //53=7
    myLoader.load(Request5);
    }
    
    }
    Cheers!

    -Llyfre

  4. #4
    Senior Member
    Join Date
    Jan 2010
    Posts
    141
    If you use myLoader.unloadAndStop(); it should also kill the sound. But either way works. Glad I could help! I know how frustrating this stuff can be.

  5. #5
    Senior Member
    Join Date
    Apr 2003
    Location
    Seattle
    Posts
    177
    Even better + keeping me learning.

    Cheers!

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