Need help with Arrays!

Using the following array as an example:
Array = [“Item1”,“Item2”,“Item3”,“etc”];

I am trying to figure out how to pull items from an array randomly. How do I setup the AS so when you click a button it will select a randon item from the array and display that item in dynamic text box? Sorry dont know AS that well but trying to learn.

Thanks for the help!

Hmm, untested, but try this as an example.

Create a new file, create a dynamic text box, give it the var name “setArray” (no quotes).

Add these actions on the frame…

myArray = ["Item1", "Item2", "Item3", "etc"];
textVar = Math.floor(Math.random()*myArray.length);
setArray = myArray[textVar];

Now to change it with a button try this…

On the Frame…

myArray = ["Item1", "Item2", "Item3", "etc"];
changeVar = function () {
	textVar = Math.floor(Math.random()*myArray.length);
	_root.setArray = myArray[textVar];
};

Where the text box has the var name setArray.

Now on the button add this…

on (release) {
	changeVar();
}

Again untested, so let me know how it goes.

Well, it depends if you want to pull the elements of the array one by one until the array is empty (a bit of array manipulation there), or if you want to take 1 randomly like Lost did (you should explain that code a bit, by the way :)).

pom :cowboy:

I just want to take 1 randomly. i tried the code lost posted but didnt seem to work. I will try playing with it some more tomorrow but not knowing AS that well its hard to troubleshoot te code…Thanks for the help guys!

My code worked for me :-\

I posted a fully commented .fla file of what I did.

Ilyas: I wasn’t even sure if it worked, I just kind of winged it, so I was going to explain it later…lol.

If you don’t understand something byis, just ask :slight_smile:

Thanks Lost! I was putting the ‘setArray’ as an instance name instead of a var and thats why it was not working. Havnet had a chance to study the code yet but im sure I might have some questions…Thanks agian!

No problem :slight_smile:

As I said… feel free to ask :slight_smile:

Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? [SIZE=1]lost? :beam:[/SIZE]

Hrmmm, I guess that depends on what needs to be written about them.

My knowledge of them is just pretty basic :-\ :frowning: :*(

*Originally posted by ilyaslamasse *
**Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? [SIZE=1]lost? :beam:[/SIZE] **

Here is a good one, macromedia technote: :slight_smile:

http://www.kirupaforum.com/showthread.php?s=&postid=61335#post61335

That’s a brilliant tutorial, h88. I put it in the best of Kirupa. [SIZE=1]I just hope people will read it…[/SIZE] :-\

That is a nice article. My problem with arrays is not understanding the array itselfs but how to manipulate them to do what you want…That article goes pretty in depth into that however.

On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on actionscript.org and didnt see anything…they do have some nice tuturiols though.

*Originally posted by byis *
**On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on actionscript.org and didnt see anything…they do have some nice tuturiols though. **

If you use MX you could take a look at the AS reference. (its the little book by the expert/normal mode button)

if you scroll down there is a “yellow” book called index, open that up, and it is an alphabetizied list. its a pretty dry read, but there are a ton there, good info too, like which versions support a certain command and whatnot.

That is what I used to learn arrays.

I learned which commands were used with Arrays and tested how they worked… trial and error really.

That is how I learn most everything I do in AS. Just try something and keep going with it until you think you have a firm grasp on it.

Math.floor(Math.random()*myArray.length);

can someone explain this statement. I kinda have a “grasp” on it, but
i don’t want to go on, if I don’t have it right.

array.length is how many elements in the array. (lets use 5 for my example.)
math.random() returns a random number greater than or equal to zero, and less than 1.
and floor will take the number from this expression and change it to the number that is equal to or rounded down.
so if math.random gave us .3345 and length was 5. that ==1.6725
then floor turns that into 1
and then that would return the 2nd element in the array.
right?

ok, so if that is right, wouldn’t it seem that the last element in the array would
be chosen the least amount of times???

random() is deprecated, not even used.

Math.floor rounds down as you said, that is the only way to get to 0, which is where an array starts counting from.

So Math.floor has the chance to round down to 0, and multiplying it by the length of the array makes sure that it has a chance of hitting that last one as well without going past it and getting a blank (null) value.

when i mentioned random I meant math.random.
probably should have put that to save confusion.

Oh ok, wasn’t sure since random() and Math.random() are different.

thanks for your help so far. one last thing.
getting back to what I said earlier wouldn’t it seem that logicallly if flash was pulling (math)random numbers and then getting the floor of those numbers, that randomly generating the last element would happen the least amount of times?