4 Replies Latest reply on Apr 19, 2006 8:11 PM by sneakyimp

    how to capture events for movies before they load

    sneakyimp
      I'm looking for some advice from an experience action scripter here....am i going about this the right way?

      i have a MAIN.swf that has two movie placeholders on it, MC1 and MC2. Depending on the context, I load different SWFs into MC1 and MC2. They are usually datagrids and stuff. In one case, I load the same SWF into both movieclips. The idea is to re-use code rather than having a million SWFs that basically do the same thing.

      The problem i'm having is that i want to allow drag and drop of the data in MC2 onto MC1 and have MC1 respond accordingly. This is a problem because I cannot refer to components in a movie before it has been loaded. I am not certain (please correct me) but I believe that you can't do this:

      MC1.onLoad = function() {
      trace('MC1 loaded');
      }
      MC1.loadMovie("1.swf");

      the reason being that your onLoad function is immediately obliterated when you load the movie. As I understand it this also is not possible becaus the movie might onLoad event might fire before you've defined your onload function:

      MC1.loadMovie("1.swf");
      MC1.onLoad = function() {
      trace('MC1 loaded');
      }

      I'm currently handling this by putting a setup function in MC1 and MC2 depending on what I want the interaction between the two movies to be. something like this:

      // this is in MAIN.SWF
      MC1.content_mc.loadMovie("1.swf");
      MC1.setupFN = function() {
      // 'this' now refers to MC1
      this.gridListener = new Object;
      this.gridListener.change = function(evt) {
      var gs bject = evt.target.selectedItem;
      // LOAD DATA INTO OBJECT IN MAIN.SWF
      }
      this.content_mc.dataGrid.addEventListener("change", this.gridListener);
      }

      then inside 1.swf, I have this in the first frame which assigns event handlers to my data objects.
      this._parent.setupFN();

      This basically works. even if I load the same swf file into MC1.content_mc and MC2.content_mc, i can assign different event handlers to dataGrid in each instance of the same SWF.

      HOWEVER I HAVE THIS PROBLEM
      That the setupFN() function in MC1 needs to refer to objects in MC2 and vice versa and i have no guarantees about which might get loaded first. Race conditions!

      Is there some simpler way to do this? Can these setup functions assign event handlers to objects that haven't yet been instantiated? Do I need to set some kind of signal?

      Complicating matters is that in one case, when a movie gets reloaded, it was piling up numerous listeners so that a mouseup might execute the same function 10 times!

      Any advice would be much appreciated.
        • 1. Re: how to capture events for movies before they load
          kglad Adobe Community Professional & MVP
          create parent movieclips of MC1 and MC2 and assign handler to the parents. load your swfs into theParent1.MC1 and theParent2.MC2.

          you can assign handlers to theParent1 and theParent2 whenever you want and those handlers can define actions for their children.
          • 2. Re: how to capture events for movies before they load
            sneakyimp Level 2
            That is basically what I am doing, except that MC1 and MC2 *are* the parent clips of a movie called content_mc inside each of them. My problem is that the movie getting loaded into MC1.content_mc has to refer to a component in the SWF that is getting loaded into MC2.content_mc and vice-versa.

            I need the MC1.setup function and the MC2.setup functions to run ONLY when both MC1.content_mc and MC2.content_mc are *both* loaded.

            Either that or I need to be able to create an event handler for a component that has not yet been loaded. Is there a way to refer to a component in a movie before you are sure that it has been loaded?
            • 3. Re: how to capture events for movies before they load
              kglad Adobe Community Professional & MVP
              then define a loop in MC1 or MC2 that checks whether loading is complete both MC1.content_path and MC2.content_path, and when it is, terminate the loop and call your setup functions.
              • 4. Re: how to capture events for movies before they load
                sneakyimp Level 2
                I was afraid it was something like that. intervals make me a bit nervous because i don't fully understand what scope they exist in and they keep running until you kill them.

                I was sort of imagining some kind of signaling process whereby each would try to run their setupFN and if conditions were not right, would set a flag somewhere so that when the other setupFN ran, it would know to do some extra work to finish setup for the other movie clip. that became confusing too so I bit the bullet and did an interval thing. It seems to be working. If anyone sees potential problems, i'd love to hear about it.