4 Replies Latest reply on Jul 14, 2006 4:17 PM by SPGAnne

    probelm with listeners for components in a dynamically loaded swf

    SPGAnne Level 1
      Beginner here, so be gentle.
      I have created a .swf which has some button components on it. I have the following code on a .fla

      stop();
      import mx.controls.*;
      var layout:MovieClip = this.createEmptyMovieClip("layout",1);

      var my_mcl:MovieClipLoader = new MovieClipLoader();
      my_mcl.addListener(this);
      my_mcl.loadClip("layout.swf", layout);
      function onLoadInit(target_mc:MovieClip, status:Number):Void {
      trace("clip loaded"); // this works fine
      addListeners();
      }
      function addListeners():Void {
      trace("label of go button is " + layout.goBtn.label); // this works fine, so I can obviously address stuff by their instance name in the loaded swf
      layout.goBtn.addEventListener("click", goClicked());
      }
      function goClicked():Void {
      trace("goClicked function called");
      layout.gotoAndStop(2);
      }

      The layout.swf loads fine, but as soon as the addEventListener statement executes it calls the goClicked function. Then when I try to actually click on the go button, nothing happens (i.e. I don't get my trace statement);

      I'm sure I must be doing something truly dumb or am missing a key concept.

      Thanks in advance for anyone that can help.

        • 1. Re: probelm with listeners for components in a dynamically loaded swf
          Peter Lorent Level 2
          You missed an object that listens for the event.

          stop();
          import mx.controls.*;
          var layout:MovieClip = this.createEmptyMovieClip("layout",1);

          var btn_listener:Object=new Object();

          var my_mcl:MovieClipLoader = new MovieClipLoader();
          my_mcl.addListener(this);
          my_mcl.loadClip("layout.swf", layout);

          function onLoadInit(target_mc:MovieClip, status:Number):Void {
          trace("clip loaded"); // this works fine
          addListeners();
          }

          function addListeners():Void {
          trace("label of go button is " + layout.goBtn.label); // this works fine, so I can obviously address stuff by their instance name in the loaded swf
          btn_listener.click=goClicked;
          layout.goBtn.addEventListener("click", btn_listener);
          }

          function goClicked():Void {
          trace("goClicked function called");
          layout.gotoAndStop(2);
          }
          • 2. Re: probelm with listeners for components in a dynamically loaded swf
            SPGAnne Level 1
            Thank you again LuigiL!

            I think I just had an aha. I had copied some code from another test .fla I had made where the buttons were on the test .fla's timeline. In that case I was able to use btninstance.addEventListener("click", function name) and all worked just fine. However I guess the conclusion is that when the button instance is NOT on the same timeline where the function code you want to execute is, you need to set up that listener object to connect the 2 timelines -i.e. to make sure the click gets passed from the embedded .swf timeline to the main timeline. I don't understand why when I didn't do this that the goClicked function was called as soon as I added the listener, but maybe I don't need to really know why I got this strange effect, just need to know how to set it up correctly to begin with.

            But....now I'm stuck on the next thing. My layout.swf that I dynamically load has 2 frames. When I click the go button I go to the second frame of the layout.swf where I have a back button and a text box. In my goClicked function on the tester timeline, I have no problems addressing the text box and putting some text in it, but I can't seem to address the back button. I can't for example trace the label property and since I can't do that, I'm sure my attempts to addEventListeners is failing too. When I do an enumeration of the properties on that frame, it lists the backBtn instance, and when I put trace code on the .swf itself, when it gets to frame 2 it can trace the label of the backBtn. So why from my test .fla can I not address it?

            Again your patient help is much appreciated.
            • 3. Re: probelm with listeners for components in a dynamically loaded swf
              Peter Lorent Level 2
              The back button is undefined on frame 1 of the layout fla. Fix: put the back button on frame 1 (and 2) and set it to invisible in the code. On frame 2 set it to visible in the code.
              back_btn.visible=true;

              Here is another way to handle multiple events with the same name.

              • 4. Re: probelm with listeners for components in a dynamically loaded swf
                SPGAnne Level 1
                Thanks again for the excellent example. I like the delegate stuff, making things more compact. I also did a little bit of head bashing against the proverbial brick wall before I learned the hard way that it is not only necessary to have all the button components defined on frame 1 of the swf, but it is CRITICAL not to have another key frame defined on the same layer as those components. I had specified a key frame for frame 2. And even though all the components ware still on that second key frame with the same names, the listeners didn't seem to hear anything. Yow.

                Interestingly, this need to have all the COMPONENTS defined on frame 1 must be unique to components, since I have no problems addressing a text field (non component element) that is ONLY on frame 2 of the swf.