Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.58 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <!--
  2. //////////////////////////////////
  3. //Buzzkill
  4. //By: Haroon Khalid
  5. //Date: 8/9/10
  6. /////////////////////////////////
  7. -->
  8.  
  9. <!doctype html>
  10. <html>
  11. <head>
  12. <title>Buzzkill</title>
  13. <meta charset="UTF-8" />
  14.  
  15. </head>
  16. <body>
  17. <section>
  18.  
  19. <div>
  20.         <canvas id="canvas" width="320" height="480">
  21.                 This text is displayed if your browser does not support HTML5 Canvas.
  22.         </canvas>
  23. </div>
  24.  
  25. <script type="text/javascript">
  26.  
  27. var canvas;
  28. canvas = document.getElementById('canvas');
  29. context2D = canvas.getContext('2d');
  30.  
  31. //Create Shot class for when the player shoots
  32. function Shot (type) {
  33.         this.x = 150;
  34.         this.y = 350;
  35.         this.img = new Image();
  36.         this.img.src = "shot.png";
  37.        
  38.         // Move the shot up and draw it
  39.         this.update = function() {
  40.                
  41.  
  42.                         this.y -= 4;
  43.                         context2D.drawImage(this.img, this.x, this.y);
  44.  
  45.         }
  46. }
  47.  
  48. // Create player class
  49. function Player (type) {
  50.         this.x = 150;
  51.         this.y = 420;
  52.         this.img = new Image();
  53.         this.img.src = "player.png";
  54.         this.shots =     new Array();
  55.        
  56.         // When the player shoots
  57.         this.create_shot = function() {
  58.                 this.s = new Shot();
  59.                 this.shots.push(this.s);
  60.         }
  61.        
  62.         // For all the shots fired, draw them in thier record position from the array
  63.         this.update_shots = function() {
  64.                
  65.                 for (index = 0; index < this.shots.     length; index++) {
  66.                         this.shots[index].update();
  67.                         if (this.shots[index].y < 100) {
  68.                                 //how do I get the position of the element to delete it from the array in the ()
  69.                                 this.shots.splice(index, 1);
  70.                                 console.log(this.shots.length);
  71.                                
  72.  
  73.                         }
  74.                 }
  75.         }
  76.        
  77.         //  Update all the shots and draw the player
  78.         this.run = function() {
  79.                 context2D.clearRect(0,0,320,480);
  80.                 context2D.fillStyle = "#000000";
  81.                 context2D.rect(0,0, 320, 480);
  82.                 context2D.fill();
  83.                 context2D.closePath();
  84.                 this.update_shots();
  85.                 context2D.drawImage(this.img, this.x, this.y);
  86.         }
  87. }
  88.  
  89. function clear() {
  90.   context2D.clearRect(0, 0, 320, 480);
  91. }
  92.  
  93. function init() {
  94.     canvas = document.getElementById("canvas");
  95.     ctx = canvas.getContext("2d");
  96.     return setInterval(draw, 10);
  97. }
  98.  
  99. function doKeyDown(evt){
  100.         switch (evt.keyCode) {
  101.                 case 32:  /* Up arrow was pressed */
  102.                         p1.create_shot();
  103.                         break;
  104.                 case 37:  /* Left arrow was pressed */
  105.                         p1.x -= 5;
  106.                         break;
  107.                 case 39:  /* Right arrow was pressed */
  108.                         p1.x += 5;
  109.                         break;
  110.                 }
  111. }
  112.  
  113. //Where do I create the player?'
  114. var p1 = new Player();
  115.  
  116. //Clear the screen and then draw the player
  117. function draw() {
  118.         clear();
  119.         p1.run();
  120. }
  121.  
  122. init();
  123.  
  124. // Poll for keyboard events?
  125. window.addEventListener('keydown',doKeyDown,true);
  126.  
  127. </script>
  128. </section>
  129. </body>
  130. </html>