marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.01k stars 794 forks source link

Character 16-Canvas #62

Closed xfry closed 10 years ago

xfry commented 10 years ago

Hi! First i want to give thanks for share your knowledge with us, you are making an amazing work with this book.

But I have been following the character 15-16 of the book and i found some bugs or maybe typos, but none stopped me to continuing.

I have found at this lines 915 - 916 that maybe you forgot put the .length properties in the for loop, and then like drawback the background is not drawing the whole tiles around the scene. check out the current code: https://github.com/marijnh/Eloquent-JavaScript/blob/master/16_canvas.txt#L915-L916

I have fix it bug in the following code below

CanvasDisplay.prototype.drawBackground = function() {
  var view = this.viewport;
  var xStart = Math.floor(view.left);
  var xEnd = Math.ceil(view.left + view.width);
  var yStart = Math.floor(view.top);
  var yEnd = Math.ceil(view.top + view.height);

  var level = this.level.grid;
  for (var y = yStart; y < level.length;   y++) {
    for (var x = xStart; x < level[y].length; x++) {
      var tile = this.level.grid[y][x];
      if (tile == null) continue;
      var screenX = (x - view.left) * scale;
      var screenY = (y - view.top) * scale;
      var tilePos = tile == "lava" ? scale : 0;
      this.cx.drawImage(otherSprites,
                        tilePos,       0, scale, scale,
                        screenX, screenY, scale, scale);
    }
  }
}; 

One more time, so much thanks for share your knowledge wit us. I finish all the character between 15-16 and have the game running successful with Canvas :v:
eloquentjavascript-game

marijnh commented 10 years ago

Hi Fredy,

The code works fine for me as it is, and looks like it is correct. What problem, exactly, does your change solve?

Best, Marijn