Jump to content

Randomating PHP and JavaScript


mattal999

Recommended Posts

Im not sure which forum to put this in but i need help.

 

This is javascript and i need it to randomate the newpos[0] value:

 

<script language="JavaScript">
pos = new Array();
pos[0] = "60,60";
pos[1] = "65,60";
pos[2] = "70,60";
pos[3] = "75,60";
pos[4] = "80,60";
pos[5] = "85,60";
pos[6] = "90,60";
pos[7] = "95,60";
pos[8] = "100,60";
pos[9] = "105,60";
pos[10] = "110,60";
pos[11] = "115,60";
pos[12] = "120,60";
pos[13] = "125,60";
pos[14] = "130,60";
pos[15] = "135,60";
pos[16] = "140,60";
pos[17] = "145,60";
pos[18] = "160,60";
pos[19] = "165,60";
pos[20] = "170,60";
pos[21] = "175,60";
pos[22] = "180,60";
pos[23] = "185,60";
pos[24] = "190,60";
pos[25] = "195,60";
pos[26] = "200,60";
pos[27] = "205,60";
pos[28] = "210,60";
pos[29] = "215,60";
pos[30] = "220,60";
pos[31] = "225,60";
pos[32] = "230,60";
pos[33] = "235,60";
pos[34] = "240,60";
pos[35] = "245,60";
pos[36] = "250,60";
pos[37] = "255,60";
pos[38] = "260,60";
pos[39] = "265,60";
pos[40] = "270,60";
pos[41] = "275,60";
pos[42] = "280,60";
pos[43] = "285,60";
pos[44] = "290,60";
pos[45] = "295,60";
pos[46] = "300,60";
pos[47] = "305,60";

var step = 0;
var laststep = pos.length;

var timer;

function animate()
{
   step++;
   if(step == laststep) step = 0;

   var newpos = pos[step].split(",");

   // Here is where i need it to randomate the newpos[0] key. I want it to randomate min=4 below newpos[0] and max=4 above newpos[0] example: 
   // <? rand("(newpos[0]-4)","(newpos[0]+4)"); ?>

   window.document.all.ball1.style.left = parseInt(newpos[0]);
   window.document.all.ball1.style.top = parseInt(newpos[1]);
   timer = setTimeout("animate()",100);
   if(newpos[0] == 305) clearTimeout(timer);
}

function stopAnimation()
{
   clearTimeout(timer);
}

</script>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/
Share on other sites

It is indeed javascript. And it's also randomize not randomate. Anyways, this little bit should do it for you:

 

rand = Math.floor(Math.random()*47+1);
randomized = pos[rand];

 

So the variable randomized will contain the element you want.

i put this:

 

function animate()
{
   step++;
   if(step == laststep) step = 0;

   var newpos = pos[step].split(",");
   
   rand = Math.floor(Math.random()*47+1);
   randomized = pos[rand];
   newpos[0] = randomized;
   
   window.document.all.ball1.style.left = parseInt(newpos[0]);
   window.document.all.ball1.style.top = parseInt(newpos[1]);
   timer = setTimeout("animate()",100);
   if(newpos[0] == 305) clearTimeout(timer);
}

function stopAnimation()
{
   clearTimeout(timer);
}

</script>

 

And it goes haywire! i want it to get newpos[0] and randomate it with minimum number of newpos[0]-4 and max of newpos[0]+4

well, i do get an error sometimes... "'pos[...]' is null or not an object?"

 

my code now:

 

<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript">
pos = new Array();
pos[0] = "60,60";
pos[1] = "65,60";
pos[2] = "70,60";
pos[3] = "75,60";
pos[4] = "80,60";
pos[5] = "85,60";
pos[6] = "90,60";
pos[7] = "95,60";
pos[8] = "100,60";
pos[9] = "105,60";
pos[10] = "110,60";
pos[11] = "115,60";
pos[12] = "120,60";
pos[13] = "125,60";
pos[14] = "130,60";
pos[15] = "135,60";
pos[16] = "140,60";
pos[17] = "145,60";
pos[18] = "160,60";
pos[19] = "165,60";
pos[20] = "170,60";
pos[21] = "175,60";
pos[22] = "180,60";
pos[23] = "185,60";
pos[24] = "190,60";
pos[25] = "195,60";
pos[26] = "200,60";
pos[27] = "205,60";
pos[28] = "210,60";
pos[29] = "215,60";
pos[30] = "220,60";
pos[31] = "225,60";
pos[32] = "230,60";
pos[33] = "235,60";
pos[34] = "240,60";
pos[35] = "245,60";
pos[36] = "250,60";
pos[37] = "255,60";
pos[38] = "260,60";
pos[39] = "265,60";
pos[40] = "270,60";
pos[41] = "275,60";
pos[42] = "280,60";
pos[43] = "285,60";
pos[44] = "290,60";
pos[45] = "295,60";
pos[46] = "300,60";
pos[47] = "305,60";

var step = 0;
var laststep = pos.length;

var timer;

function animate()
{
   step++;
   if(step == laststep) step = 0;

   var newpos = pos[step].split(",");
   rand = Math.floor(Math.random()*47+1) % 4;
   var newpos = pos[step + rand].split(",");   
   window.document.all.ball1.style.left = parseInt(newpos[0]);
   window.document.all.ball1.style.top = parseInt(newpos[1]);
   
   var thatpos = pos[step].split(",");
   rand = Math.floor(Math.random()*47+1) % 4;
   var thatpos = pos[step + rand].split(",");   
   window.document.all.ball2.style.left = parseInt(thatpos[0]);
   window.document.all.ball2.style.top = parseInt(90);
   
   timer = setTimeout("animate()",100);
   if(newpos[0] >= 301) window.location="won.php"; 
   if(newpos[0] >= 301) clearTimeout(timer);
   if(thatpos[0] >= 301) window.location="lost.php";
   if(thatpos[0] >= 301) clearTimeout(timer);

}

function stopAnimation()
{
   clearTimeout(timer);
}

</script>

</head>
<body onload='animate();' bgcolor="#333333">
<span id="ball1" style="position:absolute; visibility: visible; top:60; left:60; width:32; height:32; z-index:2">
<img border=0 width=32 height=15 src="../My%20Pictures/car.bmp"></span><span id="ball2" style="position:absolute; visibility: visible; top:90; left:90; width:32; height:32; z-index:2">
<img border=0 width=32 height=15 src="../My%20Pictures/car.bmp"></span>
<font color='ffffff'>
<span id="finish" style="position:absolute; visibility: visible; top:36; left:324; width:32; height:148; z-index:2">
<font face="Verdana">| F<br>| I<br>| N<br>| I<br>| S<br>| H</font><br></span>
</body></html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.