7 Replies Latest reply on Jul 5, 2007 7:06 PM by Flashie_Noob

    Help with an array for many MC buttons

      Hello. I am a very new flash user and am working on a flash project that involves as many as 50 movie clip buttons.

      Each button will look different so I plan on making 50 buttons in the library. Each button will have 4 states - Up, Down, Upover, & Downover. There are 4 keyframes on the button, 1 for each state, with a stop() action on each keyframe.

      The action script I have for 1 button is attached at the end.

      Ok. Here is where I am stuck. What I want to do is use an array to store the true/false value of each of my buttons so that hopefully I do not have to copy and paste this code 50 times and change the name of the button. It seems that I should be able to use 1 section of code to change the state of whatever button I press. I simply do not understand arrays and how to make a MC reference an array.

      Anyone mind helping me out a bit?
        • 1. Re: Help with an array for many MC buttons
          shikozel Level 1
          if you have this array that store the name of your btns:
          aBtnNames=["btn0","btn1", "btn2"..."btnN"]

          you can do that:
          for (var i:Number=0;i<aBtnName.length;i++)
          //get the instance of the btn

          now you should assign the action to myBtn

          • 2. Re: Help with an array for many MC buttons
            Level 7
            This should get you started:

            // AS2:

            var buttonArray:Array = new Array(btn1,btn2,btn3);
            // add each of the button instance names to the array

            // this loop will create the toggled property and add the mouse event
            functions to each of the button instances...
            for (i in buttonArray) {
            buttonArray .toggled = false;
            .onPress = buttonPress;
            buttonArray .onRollover = buttonRollOver;
            .onRelease = buttonRelease;

            // then write one function for each event, use this as a model for the
            other functions...
            function buttonPress() {
            this.toggled = ! this.toggled;
            if (this.toggled) {
            } else {
            • 3. Re: Help with an array for many MC buttons
              Flashie_Noob Level 1

              There are a few things I do not understand.

              First part:

              var buttonArray:Array = new Array(btn1,btn2,btn3);

              Ok. That I get. But I think I need it to be:
              var buttonArray:Array = new Array("btn1", "btn2","btn3");
              When I did it without the quotes, the array had 0, 1, & 2, but there were no values in them.


              for (i in buttonArray) {
              buttonArray.toggled = false;
              buttonArray.onPress = buttonPress;
              buttonArray.onRollover = buttonRollOver;
              buttonArray.onRelease = buttonRelease;

              This part is confusing. When I debug the code, I see the following under the variables for buttonArray:
              0 = "btn1"
              1 = "btn2"
              2 = "btn3"
              onPress = undefined
              onRelease = undefined
              onRollOver = undefined
              toggeled = false

              As I understand it, toggled is a specific variable under buttonArray and does not appear to be a state of each of the different names. Also, should onPress, onRelease, and onRollOver be defined as function calls then? Is it like:
              buttonArray.onPress = buttonPress();

              On my quick test, the buttons didn't work. Since the value of the array is the name of the buttons and I do not see that the toggled value is stored per button, I do not see how this will work.
              • 4. Re: Help with an array for many MC buttons
                Flashie_Noob Level 1
                I thought about this last night and am confused as to why I need to store the button names at all in the array. Let me know if this might work:

                Define an array btnToggled with 51 empty values.
                run a loop to set each value to false

                ok. for btn1, I am looking at a variable called btn1.toggled and this.toggled which would match. If instead I trimmed the btn off of the button name I am on and used the number for the btnToggeled array, would that work? I do not know if I can trim the button name and the this command to eliminate the letters. If so:

                //Add a step to trim the button you are pressing and assign that number to i. i=1 for btn1. This defines what button you are on, pressing, or rolling off of. Not sure how to do it.

                btn1.onPress = function(){ //Not sure how to name the function here. Probably need to name a new variable and set it to btn+i. I think shikozel was trying to do that with mybtn.

                btnToggled = !btnToggled
                if(btnToggled == true){

                what do you think?
                • 5. Re: Help with an array for many MC buttons
                  Level 7
                  Let's back up a step or two. This actionscript as I wrote it is intended
                  to be placed in a frame script space on the same timeline as the buttons
                  themselves. Each button instance should have an instance name, "btn1",
                  etx. I'm guessing that you did that. These button instances should be in
                  the same frame as the actionscript. In other words, if the buttons exist
                  in frame 1, then the AS should also be in frame 1.

                  1. The array of the button instance names should have a relative path to
                  each of the buttons. The names should not have quotes around them. The
                  reference is to the instance path and name. So, if the buttons are in
                  another movieClip then you'd have to include the clip's name with the
                  name of each button: e.g. (buttonClip.btn1,buttonClip.btn2, etc). This
                  is essential for the second part to work correctly.

                  The for loop will use the button references in the array and assign the
                  toggled property and its value to each button, then it will assign each
                  of the functions to each of the buttons. This is what you were after,
                  one function that will work for each of the events for each of the

                  Since the reference to the button instances is wrong, everything else
                  will fail.

                  Have a look at this example,
                  http://www.ddg-designs.com/downloads/arraySample.zip, this should help
                  to explain the process. In this example the buttons are in a movieClip.
                  • 6. Re: Help with an array for many MC buttons
                    Level 7
                    The point of creating the array and storing the button's instance names
                    is that this is the reference point for everything else.

                    When the code is read, the array is instanciated. You now have a listing
                    of each of the button names that you want to operate on. Then in the for
                    loop, you set functions and a property for each of those buttons.

                    All of this happens only once. When the code is first read by the
                    player. Once this code has been read, each of the functions exist and
                    can then be used for the rest of the time that the movie is running.

                    I'm really not following what you're proposing here.
                    • 7. Re: Help with an array for many MC buttons
                      Flashie_Noob Level 1
                      Wow. I barely understand what is going on in the actionscript. I am currently studying why it works. I tried this without the quotes on the button names but I must have had something else wrong when I did it. You are absolutly right in how this works. It does exactly what I was trying to do.

                      Thank you so much for your help.