Skip navigation
Currently Being Moderated

Indexing an entire array at once?

Apr 26, 2012 10:16 PM

Tags: #arrays

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?

 
Replies
  • Currently Being Moderated
    Apr 27, 2012 1:08 AM   in reply to witherton

    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");

                                            }

                                  }

                        }

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 7:54 PM   in reply to witherton

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 4:33 AM   in reply to witherton

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 2:31 PM   in reply to witherton

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points