5 Replies Latest reply on Aug 30, 2006 4:26 AM by jim_distinct

    Event Handlers for Dynamic Buttons

    charmcityMD
      Hi, I hope someone can help. I just can't figure out how to solve this problem:

      I am creating an number of buttons dynamically that correspond each to a filename read from a directory. The problem is I don't know ahead of time how many buttons (files) I will have so I create them dynamically in action script. -- this much I can do. What I can't figure out is how do I assign each of these dynamically created buttons individually specific event handlers? ie. I need to know which file icon you clicked on and then take action based on that info.

      for example: "folder" is a button icon I have linked from my library - i name each instance "folder"+i -- what I need to do is do something for folder1, folder2 etc.

      for (i=1;i<numdirs;i++) {
      graphics.attachMovie("folder","folder"+i,depth);
      graphics["folder"+i]._y= coordy2;
      // want to put an event handler here for each graphics["folder"+i] button
      }

      I wanted to do something like:
      graphics["folder"+i].onRelease = function() {
      test_txt.text = i;
      };
      but each button I click on gives me i= numdirs (ie the max number -- for example 6 if there are 6 files, I can't identify which button you actually clicked on -- button1 through 6 all deliver the number 6 to test_txt.text)

      I hope that makes sense - if someone can take a swag at this and point me in the right direction I will be very appreciative.

      Best Regards,
      Tom
        • 1. Event Handlers for Dynamic Buttons
          jim_distinct
          Hi.

          The problem you are having is that i will always be numdirs as it has incremented to that value within the loop.

          try something like this.

          for (i=1;i<numdirs;i++) {
          graphics.attachMovie("folder","folder"+i,depth);

          // this line adds i as custom property of the movieclip
          graphics["folder"+i].id = i;

          graphics["folder"+i]._y= coordy2;
          // want to put an event handler here for each graphics["folder"+i] button

          graphics["folder"+i].onRelease = function() {
          // scope is this because function is called within the attached clip scope
          test_txt.text = this.id
          };
          }

          On each increment of the loop, the variable i is assigned to the movieclip, then the handler assigns the movieclip property 'id' to the text field.

          Hope that makes sense, it's very late!

          • 2. Re: Event Handlers for Dynamic Buttons
            charmcityMD Level 1
            THANK YOU soo much -- I have been pondering this and scanning the web for hours. Your solution is perfect. I very very much appreciate your help.

            Best Regards,
            Tom
            • 3. Re: Event Handlers for Dynamic Buttons
              Level 7
              There is other way to do that.. whenever you call a attachMovie or loadMovie
              function there is a return value that corresponds to the actual movieclip
              created on the flash movie. ie

              for (i=1;i<numdirs;i++) {
              var temp_mc:MovieClip = graphics.attachMovie("folder","folder"+i,depth);
              }

              that new reference temp_mc is the newly created movieclip and you can do
              whatever you want with it, ie:

              for (i=1;i<numdirs;i++) {
              var temp_mc:MovieClip = graphics.attachMovie("folder","folder"+i,depth);
              temp_mc._y = 30
              temp_mc.id = I
              //even assign new funtions to events
              temp_mc.onRelease = releaseFunction
              }

              function releaseFunction(){
              trace("do something on the release event")
              }


              <DIV>&quot;charmcityMD&quot; &lt;webforumsuser@macromedia.com&gt; wrote in
              message news:ed2l97$n8$1@forums.macromedia.com...</DIV>> THANK YOU soo
              much -- I have been pondering this and scanning the web for hours. Your
              solution is perfect. I very very much appreciate your help.
              >
              > Best Regards,
              > Tom

              • 4. Re: Event Handlers for Dynamic Buttons
                jim_distinct Level 1
                Thanks.

                You're right.

                Using a temp variable is a better more readable way to do it. And is actually the method i use myself.

                Thats what too many late nights do for you lol.
                • 5. Re: Event Handlers for Dynamic Buttons
                  jim_distinct Level 1
                  Thanks.

                  You're right. I missed that!

                  Using a temp variable is a better more readable way to do it. And is actually the method i use myself.

                  Thats what too many late nights do for you lol.