actionscript-3flashswfloader

I have two externally loaded SWF files and I would like for each to recognize that the other has loaded or !loaded?


After loading 2 SWF files via two separate buttons, I am attempting to get one to recognize that the other is being viewed and unload. What would be the best way given the code here?

function matrixLoad(event:MouseEvent): void
{
if (fl_ToLoad3)
{
    fl_Proloader3 = new fl_Proloader3();
    fl_Proloader3.load(new URLRequest("assets/matrix1.swf"));
    addChild(fl_Proloader3);
    fl_Proloader3.x = 0;
    fl_Proloader3.y = 300;
    trace("Matrix1 Load");
}
else
{
    fl_Proloader3.unload();
    removeChild(fl_Proloader3);
    fl_Proloader3 = null;
    trace("Matrix1 UnLoad");
}
fl_ToLoad3 = !fl_ToLoad3;
}

function matrixLoad2(event:MouseEvent): void
{
if (fl_ToLoad4)
{
    fl_Proloader4 = new fl_Proloader4();
    fl_Proloader4.load(new URLRequest("assets/matrix2.swf"));
    addChild(fl_Proloader4);
    fl_Proloader4.x = 0;
    fl_Proloader4.y = 300;
    trace("Matrix2 Load");
}
else
{
    fl_Proloader4.unload();
    removeChild(fl_Proloader4);
    fl_Proloader4 = null;
    trace("Matrix2 UnLoad");
}
fl_ToLoad4 = !fl_ToLoad4;
}


matrix1.addEventListener(MouseEvent.CLICK, matrixLoad);
matrix2.addEventListener(MouseEvent.CLICK, matrixLoad2);

Solution

  • To get your code working, you should avoid some errors like using a variable and a Class with the same name :

    fl_Proloader3 = new fl_Proloader3();
    

    here if you have a Class called fl_Proloader3 so your instance should have a different name (the same thing for fl_Proloader4 = new fl_Proloader4();).

    Also, I don't know how did you initialized fl_ToLoad4 and fl_ToLoad3, but normally they should have the false value at the beginning because your SWFs are not already been loaded, and in that case your if statements should be like this :

    if (! fl_ToLoad3)
    {
        // load the swf
    } 
    else 
    {
        // unload the swf
    }
    

    but you can even do all that without using Booleans by just using your Loader objects, like this :

    if (! fl_Proloader3)
    {
        // load the swf
    } 
    else 
    {
        // unload the swf
    }
    

    which can give you something like this :

    var fl_Proloader3:Loader,
        fl_Proloader4:Loader;
    
    function matrixLoad(event:MouseEvent):void 
    {
        // here we can know that the other swf is loaded or not
        if (fl_Proloader4) {
            trace("Matrix2 is Loaded");
        } else {
            trace("Matrix2 is Unloaded");
        }
    
        if (! fl_Proloader3) {
            fl_Proloader3 = new Loader();
            fl_Proloader3.load(new URLRequest("assets/matrix1.swf"));
            addChild(fl_Proloader3);
            fl_Proloader3.x = 0;
            fl_Proloader3.y = 300;
        } else {
            fl_Proloader3.unload();
            removeChild(fl_Proloader3);
            fl_Proloader3 = null;
        }
    }
    
    function matrixLoad2(event:MouseEvent):void 
    {
        // here we can know that the other swf is loaded or not
        if (fl_Proloader3) {
            trace("Matrix1 is Loaded");
        } else {
            trace("Matrix1 is Unloaded");
        }
    
        if (! fl_Proloader4) {
            fl_Proloader4 = new Loader();
            fl_Proloader4.load(new URLRequest("assets/matrix2.swf"));
            addChild(fl_Proloader4);
            fl_Proloader4.x = 0;
            fl_Proloader4.y = 300;
        } else {
            fl_Proloader4.unload();
            removeChild(fl_Proloader4);
            fl_Proloader4 = null;
        }
    }
    
    matrix1.addEventListener(MouseEvent.CLICK, matrixLoad);
    matrix2.addEventListener(MouseEvent.CLICK, matrixLoad2);
    

    Edit :

    An example :

    Hope that can help.