PDA

View Full Version : Loading Random Images From A Folder



tomyknoker
02-25-2007, 07:55 AM
Hi All,

I have a little Flash header and each time it loads I want it to load one of six images randomly, onto the stage as a background... The images will be sitting in a folder in the same directory as the Flash file so they can easily be updated, anyone ever done this? Some opinions would be much appreciated!

BLiZZaRD
02-26-2007, 02:06 AM
Put your images in an array. Use loadMovie() to select one from said array.

I will try to find some exact coding for you to use.

BLiZZaRD
02-26-2007, 03:27 AM
Okay, after some research, I found that using the array can sometimes slow it down a tad. Let's try to avoid that.

I will continue to do some research, but instead we could just tell Flash to do a random math function and self pic an image from the result.

In order to do this, you will have to change (maybe) a few things. First, create an emptyMovieClip (if you don't know how, just ask) put it on the stage and give it an instance name we will call ours "empty". Next, you will have to give your images numeric names (1.jpg, 2.jpg etc but NOT 0.jpg)

Now all you do is create the function to randomize the number like so:



MovieClip.prototype.setRand = function() {
largest= 10;//change the number to the total number of images
lowest = 1;//this should stay the same
rand = ((Math.floor(Math.random()*100)%(largest-lowest+1))+lowest);


Then we tell the movie to take that number, and apply it to the file name, along with the folder and ".jpg" to grab the image out of the server with:



loadMovie("images/"+rand+".jpg", "empty");


Then we just call the function when we need it (right away if you want ~~ probably preferred here) :



setRand();


So all in all it should look like this:



MovieClip.prototype.setRand = function() {
largest= 10;
lowest = 1;
rand = ((Math.floor(Math.random()*100)%(largest-lowest+1))+lowest);
loadMovie("images/"+rand+".jpg", "empty");
}

setRand();



Give that a try and see what you get :)