2016-04-28 79 views
0

我創建了一個小型遊戲,我在其中創建了一個正方形的表格,並且第一個點擊了左下角的一個元素丟失了。選擇表格中的最後一個元素

我在編輯程序中左下角[point:(1,1)] square的問題。我的目標是有一個綠色廣場,在我的.CSS標記#poison

#poison { 
    width: 5em; 
    height: 5em; 
    padding: 0; 
    background-size: 5em 5em; 
    background-image: url("http://imgur.com/6Apk8Rk"); 
    } 

出現,而不是常規的棕色方形我目前輸出。

我不確定如何用#poison正方形替換左下角的正方形。

這裏是我用來創建表的JS函數。

function makeTable(x, y) { 
    var gameTable = document.getElementById('gameTable'), 
    poison = document.getElementById('poison'); 

    gameTable.innerHTML = null; 
    var table = []; 
    //creates rows 
    for (let i = 0; i < x; i += 1) { 
     var row = document.createElement('tr'); 
     //appends each row to the table 
     gameTable.appendChild(row); 
     //store the amount of iterations in array trow 
     var tRow = []; 
     table[i] = tRow; 
     //create columns (cells) 
     for (let j = 0; j < y; j += 1) { 
      let cell = document.createElement('td'); //td defines a cell 
      //set x,y coordinates 
      cell.pos = { x: i, y: j }; 
      //stores table values into cell 
      table[i][j] = cell; 

      //if you click the bottom left piece of chocolate.. 
      if (i === x - 1 && j === 0) { 
       cell.onclick = function() { 
       //..restart the game 
       restartGame(); 
       } 
      //else, behave normally 
      } else { 
      cell.onclick = function() { 
      cellClick(cell); 
      } 
      } 
      //append cells to the table 
      row.appendChild(cell); 
     } 
    } 
} 

下面是相關的HTML被要求:

<fieldset id="RowColbox"> 

<label> 
    <span>Rows:</span> 
    <input name="Cols" id="cols" type="number" value="3"/> 
</label> 

    <label> 
    <span>Columns:</span> 
    <input name="Rows" id="rows" type="number" value="4"/> 
    </label> 



    <button id="addDimensions" type="button">create</button> 
</fieldset> 


<table id="gameTable"> 

</table> 

Here是我的代碼在它的全部。

+0

請添加HTML –

+0

添加了相關的HTML,全部可以在我在原崗位鏈接找到 – user3335607

回答

0

這裏是「毒藥」添加到方法左下欄

if(cell.pos.x === (x-1) && cell.pos.y === 0){ 
    cell.id = "poison"; 
} 
1

假設你的代碼尚未完成,這裏是你可以分配ID「毒」到特定的細胞的方式(例如:{x:1, y:1}):

//set x,y coordinates 
cell.pos = { x: i, y: j }; 
if(cell.pos.x === 1 && cell.pos.y === 1){ 
    cell.id = "poison"; 
} 
+0

代碼還遠遠沒有完成!感謝您的建議,當我回到家時,我會嘗試一下。 – user3335607

相關問題