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

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.25 KB  |  hits: 10  |  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. Why doesn't javascript draw the images on the canvas?
  2. function loadPacman(posX, posY) {
  3.     this.posX = posX;
  4.     this.posY = posY;
  5.     pacman = new Image();
  6.     pacman.src="http://www.frogbug.se/pacman/img/naziman.png";
  7.     pacman.addEventListener("load", function(){
  8.             canvas.drawImage(pacman, this.posX, this.posY)
  9.         }, false);
  10. }
  11.  
  12. function loadGhost(posX, posY) {
  13.     this.posX = posX;
  14.     this.posY = posY;
  15.     ghost = new Image();
  16.     ghost.src="http://www.frogbug.se/pacman/img/ghost.png";
  17.     ghost.addEventListener("load", function(){
  18.             canvas.drawImage(ghost, this.posX, this.posY)
  19.         }, false);
  20. }
  21.        
  22. function onLoad() {
  23.     var xy = document.getElementById('canvas');
  24.     canvas = xy.getContext('2d');
  25.     //calculate the x and y for canvas
  26.     x = xy.width;
  27.     y = xy.height;
  28.  
  29.     //divide the width and length of the canvas to get small cubes
  30.     //which is 40x40
  31.     xtile = Math.floor(x/40);
  32.     ytile = Math.floor(y/40);
  33.  
  34.     //draw lines around the canvas
  35.     canvas.strokeRect(0, 0, x, y);
  36.  
  37.     //the array for the map
  38.     var map = getMapArray();
  39.  
  40.     //draw the map
  41.     drawMap(map);
  42.     //fillCanvas("0010", 0, 40, 40, 40);
  43.  
  44.     //load the ghost and pacman
  45.     loadPacman(0, 0);
  46.     loadGhost((xtile*40)-40, (ytile*40)-40);
  47. }