all groups > flash actionscript > july 2005 >
You're in the

flash actionscript

group:

Dynamically load multiple external images randomly


Dynamically load multiple external images randomly FMaree
7/31/2005 9:00:28 PM
flash actionscript:
Ok guys. I'm a bit stuck and hope someone can help me.

I am building a small flash animation for a client that will be used as a
header for his website. He's a photographer and would like to show 3 random
photos that he took there. The images are all 160 x 120 and in jpg format. I
will also have them all uploaded to a seperate folder in the root of his
website called header_images. However, he doesn't want the same image to show
up in two (or all three) spots.

I have created 3 MC's and positioned them in the correct position. I named
each instance instancePhoto_01 up to instancePhoto_03. Then I use the following
code to load a random image:

_root.instancePhoto_01.loadMovie("./header_images/header_"+random(7)+".jpg");
_root.instancePhoto_02.loadMovie("./header_images/header_"+random(7)+".jpg");
_root.instancePhoto_03.loadMovie("./header_images/header_"+random(7)+".jpg");

There are currently 7 test images in the folder that's why the random(7). I
could have used a for loop to load each instance but the amount of code to do
that would have been about the same. There will always be only 3 images to
show. Now, the code works, but sometimes it loads the same images in 2 slots.
Anybody care to help this hapless soul? :)
Re: Dynamically load multiple external images randomly kglad
7/31/2005 9:13:35 PM
add the following code to your movie and list your jpgs in an array. call the
new shuffle() method for arrays and load the first 3 images of the shuffled
array:

Array.prototype.shuffle = function() {
for (var ivar = this.length-1; ivar>=0; ivar--) {
var p = random(ivar);
var t = this[ivar];
this[ivar] = this[p];
this[p] = t;
}
};
headerA=[];
for(var i=0;i<7;i++){
headerA.push("./header_images/header_"+i+".jpg");
}
headerA.shuffle();
for(var i=1;i<=3;i++){
_root["instancePhoto_0"+i].loadMovie(headerA[ i-1 ]); /* you could use i
instead of i-1 here because you have plenty of array elements */
}
Re: Dynamically load multiple external images randomly FMaree
7/31/2005 9:27:54 PM
Holy hot tomale! That was a fast response! Thanks kglad. It works like a charm.
:D

Now, let's say more images gets added to the folder over time, is there a way
to tell the 7 in the code: "for(var i=0;i<7;i++){" to become the amount of
images in the folder?
Re: Dynamically load multiple external images randomly kglad
8/1/2005 12:00:00 AM
you'll have to set that 7 to another number in the authoring environment.
flash can't figure out how many files are in a particular folder like
.../header_images.

for your client, you can set-up a text file that contains the number of images
in that folder (which your client can update as more pics are added to the
folder) and flash can retrieve that info from the text file and use it in that
for-loop.
Re: Dynamically load multiple external images randomly kglad
8/1/2005 12:00:00 AM
Re: Dynamically load multiple external images randomly FMaree
8/1/2005 8:19:53 AM
Yeah, I thought that was going to be the case. Luckilly I already read two
variables from a text file for use elsewhere so I'll just add a third one that
will have the number of images and read that in as a third variable.

Thank you very much for your help kglad.
AddThis Social Bookmark Button