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

73 Views Asked by At

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

There are 3 best solutions below

11
akmozo On BEST ANSWER

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.

0
Jamiryo On

You can dispatch an custom event whenever the swf unloaded. Then you can listen this event and the other one will be aware of the view&unload structure.

function matrixLoad(event:MouseEvent): void
{
    var customEvent:Event = new Event("SomeEventName", true);
    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();
        dispatchEvent(customEvent);//just after unload
        removeChild(fl_Proloader3);
        fl_Proloader3 = null;
        trace("Matrix1 UnLoad");
    }
    fl_ToLoad3 = !fl_ToLoad3;
}

function SomeTriggerEvent(ref:Event):void
{
    trace("swf unloaded");
}

addEventListener("SomeEventName", SomeTriggerEvent);
0
sfxworks On

I would recommend using LocalConnection. LocalConnection allows for the communication between two or more SWFs. In this case, you can have your active swf send a message to your other swf and tell it that it is currently in use.

Here's a great example on how to use LocalConnection. In your case, you can substitute the trace statement in the received message function with whatever you need to do to handle a dormant swf. Also, in your case, right before you unload the swf, you can send a message to your other (or main) swf and tell it that it is about to be unloaded.