2016-04-28 80 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

相关问题