3

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);
P.T. Fenn
  • 55
  • 8

3 Answers3

4

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.

akmozo
  • 9,829
  • 3
  • 28
  • 44
  • These were initialized by: `matrix1.addEventListener(MouseEvent.CLICK, matrixLoad);` `matrix2.addEventListener(MouseEvent.CLICK, matrixLoad2);` I will restructure as you have above and let you know how it goes. Thank you. Will post back... – P.T. Fenn Nov 23 '15 at 17:22
  • Here is the initialization: `var container:MovieClip = new MovieClip();` `addChild(container);` `container.x = 1050;` `container.y = 702;` `var dMatrix:dMatrix_mc = new dMatrix_mc();` `container.addChild(dMatrix);` `var nMatrix:nMatrix_mc = new nMatrix_mc();` `container.addChild(nMatrix);` `nMatrix.x = 110;` `container.addChild(nMatrix);` `var fl_ProLoader3:ProLoader;` `var fl_ProLoader4:ProLoader;` `var fl_ToLoad3:Boolean = true;` `var fl_ToLoad4:Boolean = true;` – P.T. Fenn Nov 23 '15 at 17:34
  • Getting errors initially, I think it may be because I hadn't originally posted my initialization lines (which are now posted). Any thoughts? I do see how you are structuring it and understand how this makes sense. Thank you in advance. – P.T. Fenn Nov 23 '15 at 18:14
  • Implicit coercion of a value of type.display:Loader to an unrelated typefl.display:ProLoader-------------------- there are 2 and they point to the `fl_Proloader3 = new Loader();` and `fl_Proloader4 = new Loader();` lines – P.T. Fenn Nov 23 '15 at 18:21
  • @P.T.Fenn You should declare `fl_Proloader3` and `fl_Proloader4` as `Loader` objects and not `ProLoader` ones ... – akmozo Nov 23 '15 at 18:22
  • we are getting somewhere with that ProLoader/ Loader change you suggested. The `trace("Matrix2 is Unloaded");` is now showing when dMatrix is selected but the SWF is not loading and the same for the nMatrix button. – P.T. Fenn Nov 23 '15 at 18:46
  • @P.T.Fenn You can use `Loader`s or `ProLoader`s but not both (I mean for declaration and initialization). Did you tried my code ? I don't see what's your problem now because my code should work according to your posted one ? – akmozo Nov 23 '15 at 18:57
  • I did try your code and it is working but the SWF that are being called are not showing. The items that show are in the comments section. They are: `trace("Matrix2 is Unloaded");` and `trace("Matrix1 is loaded");` I have attempted to place the new Loader and URLRequest inside those 'if' statements and this still does not call the swf to the stage. I have changed over all instances from ProLoader to Loader as suggested. – P.T. Fenn Nov 24 '15 at 11:45
  • Using your code above, I get this as a result in the comments window: `Matrix2 is Unloaded` and `Matrix1 is loaded` for each button respectively. I am not sure why the SWF files are not showing on stage though. Do you have any thoughts? – P.T. Fenn Nov 24 '15 at 12:01
  • @P.T.Fenn I added a little example, I changed the positions of the `Loader` objects of course ( `fl_Proloader3.x = 50; fl_Proloader3.y = 100;` and `fl_Proloader4.x = 400; fl_Proloader4.y = 100;` ) ... Take a look. I don't know why you have a problem, it's really very easy, add two buttons, and two swf in your assets dir next to your swf and that's all ... – akmozo Nov 24 '15 at 12:15
  • The problem was on my end. The SWF file name was changed in my original post for secrecy and I failed to change them back... Your code is perfect and has fixed my issue. My apologies for dragging this on. Thank you, thank you!! – P.T. Fenn Nov 24 '15 at 12:26
2

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);
Jamiryo
  • 43
  • 6
0

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.

sfxworks
  • 1,031
  • 8
  • 27