Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Random arrays

Question posted by: hollywoood69 (Newbie) on April 8th, 2008 03:08 AM
Hi I have an assignment to create a random array to store pictures. Each time that you click on one of the buttons in the header a new picture will appear. I know how to set up an array, and i also know how to setup up a random array. From what i understand an array is for data, so how do i store a picture in it. I don't know if this is making any sense, but my instructor isn't helping much either. Just a push in the right direction could help out a bunch. thanks
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
nomad's Avatar
nomad
Expert
531 Posts
April 10th, 2008
02:32 PM
#2

Re: Random arrays
Quote:
Originally Posted by hollywoood69
Hi I have an assignment to create a random array to store pictures. Each time that you click on one of the buttons in the header a new picture will appear. I know how to set up an array, and i also know how to setup up a random array. From what i understand an array is for data, so how do i store a picture in it. I don't know if this is making any sense, but my instructor isn't helping much either. Just a push in the right direction could help out a bunch. thanks


what version of actionscript are you using?
nomad

Reply
hollywoood69's Avatar
hollywoood69
Newbie
2 Posts
April 10th, 2008
08:52 PM
#3

Re: Random arrays
Quote:
Originally Posted by nomad
what version of actionscript are you using?
nomad



I am using actionscript 2.0 for now. My nex set of classes i will be using as3

Reply
Cainnech's Avatar
Cainnech
Newbie
31 Posts
May 3rd, 2008
04:29 AM
#4

Re: Random arrays
I have a similar problem.

I want to load movies randomly. I made the script, but it doesn't seem to work.

This is what I have:

Code: ( text )
  1. moviename = ["imovie1", "imovie2", "imovie3", "imovie4"];
  2.  
  3. function randomise() {
  4. _root.ilogo.play();
  5. i = moviename.length;
  6. k = Math.floor(Math.random()*i);
  7. moviename[k].play();
  8. trace(moviename[k]);
  9. }


A short explanation. All my movies are external FLV but I recall them in different movies on my main timeline. So for every FLV I have a separate movie. (the reason why I do so is because I added different cue points to the different FLV's, but that's not of any importance in this thread).

So I made an array with the instances of the different movies.
Then I randomly pick a number.

By using moviename[k] I'm displaying value k out of array moviename.

However it doesn't seem to work.

I put the trace in to check if he is actually getting the value out of the array and that is the case. So I can see him randomly picking a number but when I try to play it, he won't do it.

So the problem lies in the line: moviename[k].play();

However when I put an instance name in place of moviename[k] it does play it. Probably I'm forgetting something but I don't know what. If someone can help me, please do!

I'm using AS 2.0

Reply
axis's Avatar
axis
Newbie
12 Posts
May 20th, 2008
12:52 PM
#5

Re: Random arrays
hollywoood69:
Don't store pictures in the array, just store a reference to pictures. e.g
var pics:Array = ["pic1.jpg", "pic2.jpg", ...];
//btn is the btn you click on
btn.onPress = function()
{
//mc is the movieclip that holds your pictures
mc.loadMovie(pics[Math.random() * pics.length]);
}
_______
Cainnech:
remove inverted commas, i.e:
moviename = [imovie1, imovie2, imovie3, imovie4];

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,271 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Flash / Actionscript Forum Contributors