• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Indexing an entire array at once?

Community Beginner ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

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?

TOPICS
ActionScript

Views

989

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 28, 2012 Apr 28, 2012

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.curren

...

Votes

Translate

Translate
Advocate ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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)

                                        {

                                                  live_buttons.gotoAndStop("Win");

                                        }

                              }

                    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines