Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Image

Status
Not open for further replies.

JoeMcGarvey

Programmer
Oct 31, 2002
47
US
I would like to display a random image, or movie clip, from a group in the library.

I know enough Flash to get myself in trouble, and need to crank this out quickly.

Does anyone have a stock script that could accomplish this?
 
Here's how to accomplish this, a three step process.

1) In your library, go to each item to be displayed, and select linkage from the right-click menu. Check "export for actionscript" and give the images an ID with a number.
(Ex. image1, image2, etc).

2) Now generate a random number between 1 and the number of images you have.
Ex. (Assuming n images)
index = random()*(n-1)+1;

3) Now use the eval function to concatenate the random number to your variable name.
Ex. imageToDisplay = eval("image" + index);

4) Now if they are movie clips, load them using attachMovie.
Ex. this.attachMovie(imageToDisplay, "randomImage", 0);

Now you can manipulate your random image using the movieclip properties.

Hope that helps,
Terwin
 
Where do I put the script? In the frame or on a placeholder movieclip?
 
You could put it basically anywhere. Try it on the first frame of your main movie. It will attach a new movie clip with the instance name of "randomImage" to your _root level main movie.

But especially with MX, I would not use eval, in this manner. Use the following instead:

imageToDisplay = _root["image" + index];
Regards,

oldman3.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top