|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Rand Images from an array
Here's my code thus far (i'll denote comments with //, my original code doesn't have said comments in them, as I don't believe the C++ // comment works with JS):
<!-- Begin Rand Image Code var images=new Array ("Pictures/rand/pic1.jpg", "Pictures/rand/pic2.jpg", "Pictures/rand/pic3.jpg", "Pictures/rand/pic4.jpg", "Pictures/rand/pic5.jpg", "Pictures/rand/pic6.jpg", "Pictures/rand/pic7.jpg", "Pictures/rand/pic8.jpg", "Pictures/rand/pic9.jpg", "Pictures/rand/pic10.jpg"); var photo1=images[0]; var photo2=images[1]; var photo3=images[2]; var counter=1; //all basic stuff so far, made a new array, with a bunch of picture //URLS and created three variables assigned to the first three pics function randPic (num) { //num is equal to the number of pictures, and serves as my modus while(counter<3){ // if I do <= 3 I get huge errors, basically IE sits there until it //decides the script won't run var now = new Date(); var seed= now.getSeconds(); var rand= Math.round(Math.random() * seed) % num; //Math.round rounds to the nearest whole number //Math.random creates a random number between 0 and 1 //seed is a value between 0 and 60, depending on the seconds //on the users computer clock if(photo1!=photo2 && photo1!=photo3 && photo2!=photo3){ counter=counter+1; } if(counter=1){ photo1=images[rand]; } if(counter=2){ photo2=images[rand]; } if(counter=3){ photo3=images[rand]; } } //I know all of these if statements are being accessed, in other // words, counter eventually equals 3, and photos 1 - 3 are //picked randomly. The problem is, photo1-3 end up equaling the //same thing. I have images[6] in the first img, and I have it in all //of them. My only guess is that rand is not redefining itself with //each loop. All help is greatly appreciated. } End Rand Image Code --> <body style="background:#082984; font:10pt 'Verdana'; color:white; margin:0%"><img src="Pictures/WCP.jpg"/ onLoad="randPic(10);document.pic1.src=photo1;document.pic2 .src=photo2;document.pic3.src=photo3;"> <img name="pic1" /> <img name="pic2" /> <img name="pic3" /><br/> Thanks, EphemeralArcs Last edited by EphemeralArcs : June 13th, 2003 at 08:26 PM. |
|
#2
|
|||
|
|||
|
ok, I've figured it out somewhat. I've got the pictures picking different random numbers now, but my check to make sure that they're NEVER the same numbers is not working. Here's my code:
<!-- Begin Rand Image Code var images=new Array("Pictures/rand/pic1.jpg", "Pictures/rand/pic2.jpg", "Pictures/rand/pic3.jpg", "Pictures/rand/pic4.jpg", "Pictures/rand/pic5.jpg", "Pictures/rand/pic6.jpg", "Pictures/rand/pic7.jpg", "Pictures/rand/pic8.jpg", "Pictures/rand/pic9.jpg", "Pictures/rand/pic10.jpg"); var photo1=images[0]; var photo2=images[1]; var photo3=images[2]; var counter=1; var now; var seed; var rand; function randNum (num){ now = new Date(); seed= now.getSeconds(); rand= Math.round(Math.random() * seed) % num; } function randPic (num) { while(counter<3){ randNum(num); if(photo1!=photo2 && photo1!=photo3 && photo2!=photo3){ // THIS IS THE IF STATEMENT CHECK THAT IS GIVING ME PROBLEMS counter=counter+1; } if(counter=1){ photo1=images[rand]; randNum(num); } if(counter=2){ photo2=images[rand]; randNum(num); } if(counter=3){ photo3=images[rand]; randNum(num); } } } End Rand Image Code --> Any and all help is always appreciated. Thanks, EphemeralArcs |
|
#3
|
||||
|
||||
|
There line where you are having trouble doesn't really make sense. Why would you use a counter within an IF statment? There's no looping occuring.
That section needs a bit more thought put into the logic. Aside from that, looks like your pretty close. |
|
#4
|
|||
|
|||
|
My logic was messed up, it was supposed to be a conditional ==, not the assigning =.
Thanks though, ![]() |
|
#5
|
|||
|
|||
|
I'm having a new found trouble with this script however. This works with IE, but not Netscape. In Netscape, all it shows is broken links for the images.
I'll post the working code so you can look at it, thanks again: <!-- Begin Rand Image Code var images=new Array("Pictures/rand/pic1.jpg", "Pictures/rand/pic2.jpg", "Pictures/rand/pic3.jpg", "Pictures/rand/pic4.jpg", "Pictures/rand/pic5.jpg", "Pictures/rand/pic6.jpg", "Pictures/rand/pic7.jpg", "Pictures/rand/pic8.jpg", "Pictures/rand/pic9.jpg", "Pictures/rand/pic10.jpg"); var photo1=images[0]; var photo2=images[1]; var photo3=images[2]; var counter=1; var now; var seed; var rand; function randNum (num){ now = new Date(); seed= now.getSeconds(); rand= Math.round(Math.random() * seed) % num; } function randPic (num) { while(counter<4){ randNum(num); if(counter==1){ photo1=images[rand]; } if(counter==2){ photo2=images[rand]; } if(counter==3){ photo3=images[rand]; } if((photo1!=photo2) && (photo1!=photo3) && (photo2!=photo3)){ counter=counter+1; } } } End Rand Image Code --> :: Placed in the Body Tag :: onLoad="randPic(10);document.pic1.src=photo1; document.pic2.src=photo2;document.pic3.src=photo3 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Rand Images from an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|