8 Replies Latest reply: Apr 28, 2012 3:46 PM by witherton RSS

    Indexing an entire array at once?

    witherton

      Let's say I have an array that contains  three buttons: "button1", "button2" and "button3". The array is called "live_buttons".

       

      Along with these three buttons, there are 5 other buttons on stage that do nothing.

       

      I want the buttons in the the "live_buttons" array to cause another button to change frames when clicked.

      I wrote this function:

       

      function activateLiveClip (event:MouseEvent)

      {

       

           if(event.target.currentLabel  == (live_buttons[0])||(live_buttons[1])||(live_buttons[2]))

         

         

          { liveButton.gotoAndStop("WIn");}

      }

       

       

      It does what it is supposed to do. However, I am sure there has to be a way to avoid using a multiple conditional statements

      and just name the array, since I want to include all items in the array in the conditional statement. I have tried just typing the

      name of the array there,but it doesn't work. How do I just name the array, without indexing the individual parts of the array?

        • 1. Re: Indexing an entire array at once?
          _spoboyle Community Member

          you can't just name the array but a better way to do it might be

           

          function activateLiveClip(event:MouseEvent)

                              {

                                        for (var i:int = 0; i < live_buttons.length; i++)

                                        {

                                                  if (event.target == live_buttons[i])

                                                  {

                                                            live_buttons[i].gotoAndStop("Win");

                                                  }

                                        }

                              }

          • 2. Re: Indexing an entire array at once?
            witherton Community Member

            I did a LOT of experimenting, and it seems that adding a semi-colon with quotation marks around it gets the job done. No error comes up, so I'm wondering if there's any reason not to use it?

            • 3. Re: Indexing an entire array at once?
              Ned Murphy CommunityMVP

              It is hard to make sense of your code when you have Strings in the array and are using currentLabel properties to compare to those Strings, supposedly within the buttons themselves.  Now your mention of using a "semi-colon with quotation marks around it" makes even less sense without seeing your code.

               

              In any case, if the purpose of the code is to check to see if something is in that array, then just look into using the Array.indexOf() method... it provides a quick way to detemine if something is in an array or not... using your original code the conditional would be...

               

              if(live_buttons.indexOf(event.target.currentLabel) > -1)  ....

               

              though using event.target can be dangerous depending on your intentions,  event.currentTarget is better if you want to be sure it is the button and not something inside the button.

              • 4. Re: Indexing an entire array at once?
                witherton Community Member

                Is using a text string in an array bad practice?

                 

                What I meant by the semi-colon was that this code from what I posted earlier:

                if(event.target.currentLabel  == (live_buttons[0])||(live_buttons[1])||(live_buttons[2]))

                 

                seemed to do what I wanted it to do when I changed it to this:

                if(event.target.currentLabel  == [live_buttons])";"

                 


                 

                I'm still struggling with "indexOf". Haven't been able to find any good tutorials on Strings and the Flash help documents were confusing to me. Really trying to understand it because it's obvious that it will be beneficial to me, but for now, doing my best to figure out code at my current progress level.

                 

                 

                Thanks for the reply!

                • 5. Re: Indexing an entire array at once?
                  Ned Murphy CommunityMVP

                  Putting quotations around a semi-colon like that makes no coding sense.  Where did you see that being suggested as a solution, or did you just do it and see nothing went wrong so you assumed it does something right?  It is possible something is working right for the wrong reason.  You should trace the values of the different things involved to see why they might seem to be equal... they might both be not what they should be...

                   

                  trace(event.target.currentLabel, [live_buttons])

                  if(event.target.currentLabel  == [live_buttons])";"

                   

                  I still do not understand the relationship that should exist between "event.target.currentLabel" and an array of strings.  I also do not see how 5 do-nothing buttons fit into the description of your problem.  But as I said already, if you want to know if something is in an array, using indexOf() is the simplest way to code it.  YOu should try it outside of your design rather than let your design confuse your effort to see it work.

                  • 6. Re: Indexing an entire array at once?
                    witherton Community Member

                    Thanks for the reply, Ned!

                     

                    *Yes, I did just do the semi-colon thing after lots of experimenting.  That's why I said I had just come up with it after a lot of experimenting and asked if there was some reason not to do it in one of my previous posts.

                     

                    *The "currentLabel" tag is a mistake. I simplified the code from my project for this post, but in the project those strings are an array of frame labels not instance names. I missed that when changing the code. I specified the 5 do-nothing buttons because I was worried if someone answered, it might be with a suggestion that didn't really fit in with what I was trying to do.

                    • 7. Re: Indexing an entire array at once?
                      Ned Murphy CommunityMVP

                      It seems as though you posted all the wrong information you could have.  At this point I have no idea what the problem is, or if you even have a problem.

                      • 8. Re: Indexing an entire array at once?
                        witherton Community Member

                        All of the wrong information? You pretty much answered the 2 questions I had which were "what is a simple way to  reference an entire array?" and "is there any reason not to use the code I stumbled upon?"Looking back over the thread, it doesn't seem as if the questions got progressively more complex, but will try to be less confusing in the future Thanks for the reply!