By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Game In JavaScript

P: 1
Hi all I have a small query in preparing this game the game is about shotting the balloons means the whole page will display balloons motion from bottom to up and each balloon will have a particular arrow key(means the image of the balloon will have any of the four arrow keys ) once clicked on the arrow keys on the keyboard the balloon should be blown off or become invisible. i have managed the image of the balloons to move from bottom to top and need help on how can i get the balloons invisible when the arrow keys are pressed....

the script is written below which is used to display the balloons to move from bottom to top
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="JavaScript1.2">
  3. var no = 5; //15 image number or falling rate
  4. var speed = 5; // the lower the number the faster the image moves
  5. var snow = new Array();
  6. snow[0] = "baloon1.gif"
  7. snow[1] = "baloon2.gif"
  8. snow[2] = "baloon3.gif"
  9. snow[3] = "baloon4.gif"
  10.  
  11. var ns4up = (document.layers) ? 1 : 0;  // browser sniffer
  12. var ie4up = (document.all) ? 1 : 0;
  13. var ns6up = (document.getElementById&&!document.all) ? 1 : 0;
  14. var dx, xp, yp;    // coordinate and position variables
  15. var am, stx, sty;  // amplitude and step variables
  16. var i, doc_width = 1800, doc_height = 3000;
  17.  
  18. if (ns4up||ns6up) {
  19.         doc_width = self.innerWidth;
  20.         doc_height = self.innerHeight;
  21. } else if (ie4up) {
  22.         doc_width = document.body.clientWidth;
  23.         doc_height = document.body.clientHeight;
  24. }
  25.  
  26. dx = new Array();
  27. xp = new Array();
  28. yp = new Array();
  29. am = new Array();
  30. stx = new Array();
  31. sty = new Array();
  32. j = 0;
  33.  
  34. for (i = 0; i < no; ++ i) {
  35.         dx[i] = 0;                        // set coordinate variables
  36.         xp[i] = Math.random()*(doc_width-50);  // set position variables
  37.         yp[i] = Math.random()*doc_height;
  38.         am[i] = Math.random()*20;         // set amplitude variables
  39.         stx[i] = 0.02 + Math.random()/10; // set step variables
  40.         sty[i] = 0.7 + Math.random();     // set step variables
  41.         if (ns4up) {                      // set layers
  42.                 if (i == 0) {
  43. /*                        document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\""+ snow[j] + "\" border=\"0\"></layer>");
  44. */                        document.write("<layer name=\"dot"+ i +"\"  left=\"20\" top=\"20\" visibility=\"show\"><img src=\""+snow[j]+"\" border=\"0\"></layer>");
  45.                 } else {
  46.                         document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\""+ snow[j] + "\" border=\"0\"></layer>");
  47.                 }        } else if (ie4up||ns6up) {                if (i == 0) 
  48. {
  49.                         document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"VISIBILITY: visible; TOP: 15px; LEFT: 15px; width:1;\"><img src=\"" + snow[j] + "\" border=\"0\"></div>");
  50.                 } else {
  51.                         document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"VISIBILITY: visible; TOP: 15px; LEFT: 15px; width:1;\"><img src=\"" + snow[j] + "\" border=\"0\"></div>");
  52.                 }
  53.         }
  54.         if (j == (snow.length-1)) { j = 0; } else { j += 1; }
  55. }
  56.  
  57. function snowNS() {  // Netscape main animation function
  58.         for (i = 0; i < no; ++ i) {  // iterate for every dot
  59.                 yp[i] -= sty[i];     
  60.                        if (yp[i] < -50) {
  61.                         xp[i] = Math.random()*(doc_width-am[i]-30);
  62.                         yp[i] = doc_height;
  63.                         stx[i] = 0.02 + Math.random()/10;
  64.                         sty[i] = 0.7 + Math.random();
  65.                         doc_width = self.innerWidth;
  66.                         doc_height = self.innerHeight;                }
  67.                 dx[i] += stx[i];
  68.                 document.layers["dot"+i].top = yp[i]+pageYOffset;
  69.                 document.layers["dot"+i].left = xp[i] + 
  70. am[i]*Math.sin(dx[i]);
  71.         }
  72.         setTimeout("snowNS()", speed);
  73. }
  74.  
  75. function snowIE_NS6() {  // IE main animation function
  76.         for (i = 0; i < no; ++ i) {  // iterate for every baloon
  77.                 yp[i] -= sty[i];
  78.                 if (yp[i] < -50) {
  79. <!--                        xp[i] = Math.random()*(doc_width-am[i]-30);-->
  80.                         xp[i] = Math.random()*(doc_width-am[i]-15);
  81.                         yp[i] = doc_height;
  82.                         stx[i] = 0.02 + Math.random()/10;
  83.                         sty[i] = 0.7 + Math.random();
  84.                         doc_width = ns6up?window.innerWidth-5:document.body.clientWidth;
  85.                         doc_height = ns6up?window.innerHeight-5:document.body.clientHeight;
  86.                 }
  87.                 dx[i] += stx[i];
  88.                 if (ie4up){
  89.                 document.all["dot"+i].style.pixelTop = yp[i]+document.body.scrollTop;
  90.                 document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);
  91.                 }
  92.                 else if (ns6up){
  93.                 document.getElementById("dot"+i).style.top=yp[i]+pageYOffset;
  94.                 document.getElementById("dot"+i).style.left=xp[i] + am[i]*Math.sin(dx[i]);
  95.                 }
  96.         }
  97.         setTimeout("snowIE_NS6()", speed);
  98. }
  99.  
  100. if (ns4up) {
  101.         snowNS();
  102. } else if (ie4up||ns6up) {
  103.         snowIE_NS6();
  104. }
  105.  
  106.  
  107. </script>
  108.  
Oct 20 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
The code is old. Did you write it or is it taken from somewhere? If you want cross-browser code which is cleaner and dropping support for old, buggy browsers like Netscape 4, then you'll have to start again, though you can use a lot of the code you already have.

For key events, you need to use the onkeypress/up event to detect key presses and call a function which works out which key was pressed and deals with it accordingly. Which key was pressed?
Oct 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.