【问题标题】:How to create a html table element tilemap from an array in javascript?如何从 javascript 中的数组创建 html 表格元素 tilemap?
【发布时间】:2017-09-29 09:47:27
【问题描述】:

如何从数组中创建 html 表格 tilemap?例如,当我希望“0”成为具有绿色背景和 20px 宽度和高度的“td”,而“1”成为具有相同大小的棕色背景的“td”时?任何人都可以举一个这个数组的例子吗?另外,我想知道如何将图片插入特定的“td”元素?例如,位置表[0][0] 上的“td”元素中带有绿色背景的树图片,谢谢。

var table = [
[0,1,1,1,0],
[0,1,0,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[1,1,1,0,0]
]

【问题讨论】:

    标签: javascript html dom jstilemap


    【解决方案1】:

    var arr = [                                        // the array
      [0, 1, 1, 1, 0],
      [0, 1, 0, 0, 0],
      [0, 1, 1, 2, 2],
      [0, 0, 1, 0, 2],
      [1, 1, 1, 0, 0]
    ];
    
    var table = document.createElement("table");       // create a table element
    var tbody = document.createElement("tbody");       // create a tbody element
    table.appendChild(tbody);                          // add the tbody element to the table element
    
    arr.forEach(function(sub, j) {                     // for each sub-array sub in the array arr (j is the index of this row)
      var row = document.createElement("tr");          // create a row element
      tbody.appendChild(row);                          // add the row element to the tbody element
      sub.forEach(function(num, i) {                   // for each number num in the array sub (i is the index of this column)
        var cell = document.createElement("td");       // create a cell element
        row.appendChild(cell);                         // add the cell element to this row element
        cell.className = num === 1? "brown": "green";  // if the number num is 1 then set the class to .brown, otherwise set it to .green
        cell.id = "cell-" + i + "-" + j;               // add an id to each cell so you can select it later
      });
    });
    
    // use table as you wish
    document.body.appendChild(table);                  // in this example append it to the body
    
    // ##################################################################################
    // JUST AN EXAMPLE: (THE BELLOW CODE IS JUST TO GIVE YOU SOME INSIGHTS ON YOUR QUEST)
    // ##################################################################################
    
    var img = document.createElement("img");           // create the img element
    img.src = "http://placehold.it/15";                // set its src
    
    function moveTo(x, y) {                            // get a x and y and move the image to the cell at that pstion
      if(x < 0 || x > 4) return;                       // if x is out of bound, abort
      if(y < 0 || y > 4) return;                       // if y is out of bound, abort
    
      document.getElementById("cell-" + x + "-" + y).appendChild(img); // move the image img to cell-x-y (x from the left, and y from the top)
    }
    
    moveTo(2, 2);                                      // move to the third-third cell at start
    
    document.addEventListener("click", function(e) {   // just an example: when clicking a td element, move the image to that cell
      var target = e.target;
      if(target.tagName === "TD")
        moveTo(target.id.match(/\d+/), target.id.match(/\d+$/));
    });
    .brown, .green {
      width: 20px;
      height: 20px;
    }
    
    .brown {
      background: brown;
    }
    
    .green {
      background: green;
    }

    【讨论】:

    • 谢谢,但您能否也给我一个函数示例,该函数通过创建的表插入图片,例如绿色背景前的一棵树?例如,当我有第二个数组时,trees = [ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0 , 0, 0, 0, 0], [0, 0, 0, 1, 0] ]
    • @bady 你可以使用一个数组来处理整个事情:如果数字是1,那么颜色将为brown,否则(任何其他数字)将为green。如果数字是2,则将图像添加到单元格。检查答案,我已经编辑了。
    • 谢谢,这几乎就是我想要的,只是缺少一个小功能。我的意思是,现在我在 DOM 的表格中有图片,我只需要一个可以从表格的特定位置插入和删除图片的函数。因此,以防万一我用图片创建了这个表格,然后我想将一张图片从表格中的一个位置移动到另一个位置,然后我需要将它从最后一个位置删除并将其插入新位置。再次非常感谢。
    • @bady 不客气!检查编辑。如果您正在尝试制作游戏,最好使用 canvas
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2012-03-21
    • 2015-02-14
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多