Click to See Complete Forum and Search --> : Random images, random position?


BritMiss
08-23-2003, 09:50 AM
What I'm trying to do here is cycle different images at different places on the screen. Earlier versions of this script worked fine randomly positioning a random image but this version always writes the image to the top left of the screen. Can anybody see why? and can you suggest a solution?

Pic=new Array("Eye1.gif","Eye2.gif","Eye3.gif","Eye4.gif")

display=new Array(4)
for (x=0; x<7; ++x) { //----------------------------------------------count columns across
display[x]=new Array(4)
for (y=0; y<4; ++y) { //-------------------------------------------------and rows down
gif=parseInt(Math.random()*3) //---------------------choose a number less than 4
display[x][y]=Pic[gif] // ---------------choose a picture at random for this cell
}
}

for (x=0; x<7 ; ++x) {
for (y=0; y<4 ; ++y) {
x=parseInt(Math.random()*7) // -----------------------------random whole numbers
y=parseInt(Math.random()*4)
Top=y*100
Left=x*200

line='<DIV ID ="Eye" STYLE="position:absolute" top='+top+' left='+Left+' width=200 height=100><IMG SRC="'+display[x][y]+'" border=0></DIV>'

document.write(line)

AdamBrill
08-23-2003, 01:53 PM
Try this:<script type="text/javascript">
Pic=new Array("Eye1.gif","Eye2.gif","Eye3.gif","Eye4.gif")

display=new Array(4)
for (x=0; x<7; ++x) { //----------------------------------------------count columns across
display[x]=new Array(4)
for (y=0; y<4; ++y) { //-------------------------------------------------and rows down
gif=parseInt(Math.random()*3) //---------------------choose a number less than 4
display[x][y]=Pic[gif] // ---------------choose a picture at random for this cell
}
}

for (x=0; x<7 ; ++x) {
for (y=0; y<4 ; ++y) {
x=parseInt(Math.random()*7) // -----------------------------random whole numbers
y=parseInt(Math.random()*4)
Top=y*100
Left=x*200
line='<DIV ID ="Eye" STYLE="position:absolute; top='+Top+'; left='+Left+';" width=200 height=100><IMG SRC="'+display[x][y]+'" border=0></DIV>'
document.write(line)
}
}
</script>

BritMiss
08-24-2003, 05:32 AM
Thanks AdamBrill, IOU a beer, or bevvy of choice if we meet. BTW, I only spotted when checking your rewrite, that I had set up both loops building the array initially, to create an array 4 x 4 while the loops reading the array were trying to read 7 x 4. Thank God it's only Javascript!

AdamBrill
08-24-2003, 09:19 AM
Arrays will automatically make themselves to the right length, so you don't need to specify the length. You would be best off to specify an array like this:

display=new Array();