PDA

View Full Version : Need help with Arrays!



byis
November 23rd, 2002, 03:22 AM
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!

lostinbeta
November 23rd, 2002, 03:44 AM
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];

lostinbeta
November 23rd, 2002, 03:47 AM
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.

pom
November 23rd, 2002, 05:05 AM
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:

byis
November 23rd, 2002, 06:56 AM
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!

lostinbeta
November 23rd, 2002, 12:40 PM
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 :)

byis
November 23rd, 2002, 11:13 PM
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!

lostinbeta
November 23rd, 2002, 11:14 PM
No problem :)

As I said... feel free to ask :)

pom
November 23rd, 2002, 11:38 PM
Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? lost? :beam:

lostinbeta
November 23rd, 2002, 11:45 PM
Hrmmm, I guess that depends on what needs to be written about them.

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

h88
November 24th, 2002, 06:35 AM
Originally posted by ilyaslamasse
Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? lost? :beam:

Here is a good one, macromedia technote: :)

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

pom
November 24th, 2002, 08:03 AM
That's a brilliant tutorial, h88. I put it in the best of Kirupa. I just hope people will read it... :-\

byis
November 24th, 2002, 11:10 PM
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.

jeremy*
November 25th, 2002, 12:46 AM
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.

lostinbeta
November 25th, 2002, 12:48 AM
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.

jeremy*
November 25th, 2002, 04:24 AM
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?????

lostinbeta
November 25th, 2002, 04:33 AM
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.

jeremy*
November 25th, 2002, 05:05 AM
when i mentioned random I meant math.random.
probably should have put that to save confusion.

lostinbeta
November 25th, 2002, 05:08 AM
Oh ok, wasn't sure since random() and Math.random() are different.

jeremy*
November 25th, 2002, 05:23 AM
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?

lostinbeta
November 25th, 2002, 05:24 AM
I would think that too, but whenever I run the text, I actually get the phrase "etc" the most.

I find that weird.....not sure why it works, it works in Javascript too, that is how I knew to use it :-\

jeremy*
November 25th, 2002, 05:32 AM
-edit-

pom
November 25th, 2002, 06:57 AM
Originally posted by jeremy*
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? I don't see why it should :-\ Theoretically, Flash picks random number. So if you have 5 elements, there are equal odds that the number will be < .2 and > 0 (you get 0 ), 0.2 < number < 0.4 (--> 1), 0.4 < number < 0.6 (--> 2), 0.6 < number < 0.8 (--> 3), 0.8 < number < 1 (--> 4).

I'm not sure that this is very clear... :-\

jeremy*
November 25th, 2002, 11:03 AM
i figured it out after playing with it more, I've also be up for about 27 hours so I am/was a little out of it.

lostinbeta
November 25th, 2002, 12:45 PM
Wow, was posting that stuff at like 5:30am. :-\