2016-03-22 77 views
0

我正在學習JavaScript和我的下一個項目,我想用tileset遊戲板和一些簡單的機制創建一個簡單的RPG,雖然我可以使大多數後端功能正常工作,但我不能似乎讓我的瓷磚組正確渲染。爲什麼我的圖集無法在HTML畫布上呈現?

我目前只是試圖在tileset中的每個tile上呈現相同圖像的邏輯,但是當我嘗試渲染tileset時沒有任何反應。檢查控制檯不會顯示任何錯誤或問題,並且在搜索幾個教程後,我看不到我的渲染會崩潰並且無法正常工作,因爲我只是試圖在每個圖塊中渲染1個單個40像素x 40像素的灰色方塊。

下面是我的代碼有問題要旨的鏈接:https://gist.github.com/Randy-M/92c234a7a3cf8015bba6

和它的原貼:

//Creating the game tileset map 
 

 
var c = document.getElementById('canvas'); 
 
var canvas = c.getContext('2d'); 
 

 
var tileImg = new Image(); 
 
tileImg.src = 'sampleTile.png'; 
 

 

 
var map = { 
 
    tiles: [ 
 
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
 
     [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 
 
     [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 
 
     [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], 
 
     [41, 42, 43, 44, 45, 46, 47, 48, 49, 50] 
 
    ], 
 

 
    tSize: 100, 
 
    rows: 5, 
 
    cols: 10, 
 
    
 
    getTile: function(row, col) { 
 
     return this.tiles[row][col]; 
 
    } 
 
}; 
 

 
//testGrid used to verify the map.getTile function successfully loops through all array values 
 
var testGrid = function() { 
 
    for (r = 0; r < map.rows; r++) { 
 
     for (c = 0; c < map.cols; c++) { 
 
      var tile = map.getTile(r, c); 
 
      console.log(tile); 
 
     } 
 
    } 
 
}; 
 

 

 
var renderMap = function() { 
 
    var x = map.cols * map.tSize; 
 
    var y = map.rows * map.tSize; 
 
    
 
    for (r = 0; r < map.rows; r++) { 
 
     for (c = 0; c < map.cols; c++) { 
 
      var tile = map.getTile(r, c); 
 
      if (tile != 0) { 
 
       canvas.drawImage(tileImg, x, y, map.tSize, map.tSize, c * map.tSize, r * map.tSize, map.tSize, map.tSize); 
 
      } 
 
     } 
 
    } 
 
};
HTML section is here: 
 

 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 
</head> 
 
<body> 
 

 
<div id="mapContainer"> 
 
    <canvas id="canvas" style="height: 720px; width: 1280px;"></canvas> 
 
</div> 
 

 

 
<button id="doStuff" onclick="renderMap()">draw canvas</button> 
 

 

 
<script type="text/javascript" src="gameMap.js"></script> 
 
</body> 
 
</html>

回答

0

var c = document.getElementById('canvas'); 
 
var canvas = c.getContext('2d'); 
 
var tileImg = new Image(); 
 
tileImg.src = 'http://vignette3.wikia.nocookie.net/gtawiki/images/b/b6/Flag_of_Japan_(bordered).png/revision/latest?cb=20100321220053'; 
 

 
var map = { 
 
    tiles: [ 
 
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
 
     [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 
 
     [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 
 
     [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], 
 
     [41, 42, 43, 44, 45, 46, 47, 48, 49, 50] 
 
    ], 
 

 
    rows: 5, 
 
    cols: 10, 
 
    
 
    getTile: function(row, col) { 
 
     return this.tiles[row[col]]; 
 
    } 
 
}; 
 

 
var renderMap = function() { 
 
    var size = 30;  
 
    var x = map.cols * size; 
 
    var y = map.rows * size; 
 
    for (r = 0; r < map.rows; r++) { 
 
     for (c = 0; c < map.cols; c++) { 
 
      var tile = map.getTile(r, c); 
 
      canvas.beginPath(); 
 
      canvas.drawImage(tileImg, c * size, r * size, size, size); 
 
      canvas.stroke(); 
 
     } 
 
    } 
 
};
<button id="doStuff" onclick="renderMap()">draw canvas</button> 
 

 
<div id="mapContainer"> 
 
    <canvas id="canvas" style="height: 300px; width: 600px;"></canvas> 
 
</div> 
 

 

 
<script type="text/javascript" src="gameMap.js"></script>

我替換了日本國旗提供的圖像,因爲每個點現在代表一個成功放置的圖塊。

+0

我明白,但我真的很想看到一張圖像來填充每個圖塊。這不僅僅是獲取網格,我需要能夠通過邏輯來繪製圖像到畫布上,以便稍後可以展開並轉換爲完整/真實的圖集。 – Randy

+0

@Randy圖像現在可以工作。我改變了按鈕位置,修改了尺寸,並從'canvas.drawImage'中移除了額外的參數。這是否令人滿意? – StardustGogeta

+0

是的,這就是我一直在尋找的謝謝。除了drawImage上的額外參數,畫布上的尺寸和按鈕位置會導致問題嗎?它只是把我搞糊塗了,因爲我沒有看到任何控制檯或調試器的錯誤。再次感謝! – Randy

相關問題