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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Different Jpegs Randomly 1

Status
Not open for further replies.

Kijori

Technical User
Jan 22, 2002
67
SG
I want to create a movie clip that loads different jpegs (one at a time randomly) from a source folder. I've tried using the onClipEvent(enterFrame) but it doesn't seem to work.

Here's the onClipEvent() code;

Code:
onClipEvent(enterFrame) {
	picIndex = Math.round(1+(15*Math.random()));
	if(picIndex<10) {
		this.thumbBox_mc.loadMovie("Images/Pic 0"+picIndex+".jpg");
	} else {
		this.thumbBox_mc.loadMovie("Images/Pic "+picIndex+".jpg");
	}
}

Is there any other better way of achieving this similar effect? By the way, I would like to insert a timer too. Say... change the picture every 2 secs.
 
Using setInterval will make the timings easy:

Code:
function choosePic() {
	picIndex = Math.round(1+(15*Math.random()));
	if (picIndex<10) {
		this.thumbBox_mc.loadMovie("Images/Pic 0"+picIndex+".jpg");
	} else {
		this.thumbBox_mc.loadMovie("Images/Pic "+picIndex+".jpg");
	}
}
//call the function every 2 seconds
setInterval(choosePic,2000);

By the way you may want to look at the way you're naming your image files as having a space between the name and number will fail on some browsers.
 
Hey thanks!

I tried it and it works. My problem is, I want to create 10 duplicates of this movie clip using the duplicateMovie() function.

Although the duplicates are created, somehow the picture loading don't. How do I achieve this? I want to create many movie clips displaying different pictures randomly.
 
You can open up the choosePic function to loop through all of the dupicated clips, assuming you call them 'thumbBox_mc1' thru 'thumbBox_mc10':

Code:
function choosePic() {
	for (var i = 1; i<=10; i++) {
		picIndex = Math.round(1+(15*Math.random()));
		if (picIndex<10) {
			this['thumbBox_mc'+i].loadMovie("Images/Pic 0"+picIndex+".jpg");
		} else {
			this['thumbBox_mc'+i].loadMovie("Images/Pic "+picIndex+".jpg");
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top