0
votes

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:

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


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

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


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

1

1 Answers

0
votes

I'd go about this a different route. A much easier route in fact. Take a look at the code I wrote below. I haven't tested it, but I am pretty sure this will work

var swfs:Array = ["swf/darker.swf","swf/animalinside.swf","swf/berlin1.swf"];
var swfHolder:Sprite = new Sprite();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(e:KeyboardEvent):void
{
    //remove any previous children in swfHolder
    while (swfHolder.numChildren > 0) swfHolder.removeChildAt(0);

    //-- new loader and load the swf
    var loader = new Loader();

    switch (e.charCode)
    {
        case 51:
            loader.load(new URLRequest(swfs[0]));
            break;
        case 52:
            loader.load(new URLRequest(swfs[1]));
            break;
        case 53:
            loader.load(new URLRequest(swfs[2]));
            break;
    }
    swfHolder.addChild(loader);
}
addChild(swfHolder);