mattal999 Posted September 26, 2007 Share Posted September 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/ Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Math.random() Here's a page I got you from a google search, http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355802 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 sorry, im a php coder, making a game where a car moves across a screen in a race lol. I have no idea about javascript. Thanks anyways Maybe ill learn it! Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355804 Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355806 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 That's a whole different kettle of fish and there's another forum for that...http://www.phpfreaks.com/forums/index.php/board,6.0.html Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355807 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355808 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 use modulos (%) e,g, x % 4 It basically divides by (4) and leaves the whole remainder... Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355812 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 i dont understand... can u apply it to my code plz??? Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355815 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 randomized = pos[rand % 4]; or newpos[0] += randomized % 4; Depends upon what you want (p.s. this a plus or minus, but i'm sure you can work that one out) Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355821 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 now it stutters... ??? with the first example: keeps stuttering on first 15px or so. with the second example: like normal but it keeps going at the end... Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355825 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 You need to think a bit more, try this... rand = Math.floor(Math.random()*47+1) % 4; var newpos = pos[step + rand].split(","); Fit it in, it use's your original example and another... Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355829 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 ok it works now thanks Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355833 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Can you get it to bounce off the sides? Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355834 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355851 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Cos at times... step++; if(step == laststep) step = 0; your adding four to the max! So it's pointing to something it can't c Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355853 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Author Share Posted September 26, 2007 i dont understand..... i really need to learn javascript and i will after this php project. i just dont get it... Quote Link to comment https://forums.phpfreaks.com/topic/70767-randomating-php-and-javascript/#findComment-355856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.