Skip to main content
September 17, 2006
Question

Help with Random Image Generator

  • September 17, 2006
  • 2 replies
  • 351 views
Summary: I made a flash movie with a 'container' that will randomly load images that I've uploaded on my webspace. The images are named "image1.jpg" and "image1blur.jpg" and so forth (the blur files are used for smooth transitions.

So in the first frame of my movie, I define the maximum number of images:

------------------------------------
imageframes = 86;
rand = Math.floor(Math.random() * imageframes) + 1;
it = rand;
------------------------------------

Sure enough, the first image that's loaded into the container is a random image from #1 to #86, but after that, the image is simply incremented, whereas I want a random image to be loaded every time. Would someone please take a look at my actionscript and tell me how I can go about doing this?

Here's the code from the second frame:

------------------------------------
it += 1;
if (_root.maxpics == undefined) {
_root.maxpics = imageframes;
}
if (it > _root.maxpics) {
it -= 1;
d = 1;
while (d < it) {
swapname2 = eval(("swap" + d));
swapname2.removeMovieClip();
d++;
}
swapname3 = eval(("swap" + it));
swapname3.swapDepths(depths);
it = 1;
}
this.attachMovie("swapx", ("swap" + it), it);
swapname = eval(("swap" + it));
swapname.image = (" http://users.tpg.com.au/gbae/image" + it) + ".jpg";
swapname.imageblur = (" http://users.tpg.com.au/gbae/image" + it) + "blur.jpg";
------------------------------------

My flash file can be found at http://users.tpg.com.au/gbae/Xanga Logo.fla

Thanks in advance for any help you can offer.
This topic has been closed for replies.

2 replies

September 21, 2006
Thanks a lot for the prompt response!

Oh, I see what I've done with the incrementing thing... what should I replace that with? I've tried using the same randomising script from frame 1, but that leads to some errors (I assume that's because the number that's generated sometimes exceeds the number of images?)

"and why are you using attachMovie() and then using those attached movieclips as targets of your loadMovie()?"

To be honest I've been working with Actionscript only for a very short while, so I simply experimented with the few functions that I know until it worked out. But now that I'm not happy with the functionality, I don't really know how to go about it. Would you possibly be able to help me out, because you seem to understand my problem, and what I'm aiming for.

Thank You!
kglad
Community Expert
Community Expert
September 21, 2006
movieclips that are the target of a loadMovie() statement are replaced by the loading jpg. there's no reason to use a movieclip from your library as the target. you could use createEmptyMovieClip() to create a target movieclip and removeMovieClip() to remove a loaded jpg when it's no longer needed.

in addition, anytime you want to generate a random integer bettween 1 and 86, you can use:

kglad
Community Expert
Community Expert
September 17, 2006
your variable "it" is incremented in frame 2 (and not a random number). if frame 2 repeatedly executes you're going to load images in incremental order.

and why are you using attachMovie() and then using those attached movieclips as targets of your loadMovie()?