我尝试了很多不同的解决方案, 但我不太确定是否是问题所在 我的方法或者代码不起作用
例如
我希望能够在下面的数组中设置值,而这些值是我最初尝试设置的值
function makepointfrom(x,y,size)
{
for(var y2= y;y2 < size;y2++)
{
tilemap[x][y2] = num;
}
}
var tilemap =
[
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]
makepointfrom(2,0,4);
预期产量
var tilemap =
[
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
]
不是
var tilemap =
[
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]
答案 0 :(得分:1)
您需要将x
和y
夹在中间,因为x
是行。然后,我建议使用偏移量,并使用总和来访问元素。
function makepointfrom(x, y, size) {
for (var i = 0; i < size; i++) {
if (!tilemap[x + i]) continue;
tilemap[x + i][y] = 1;
}
}
var tilemap = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]];
makepointfrom(0, 2, 4);
tilemap.map(a => console.log(...a));
答案 1 :(得分:0)
x = innerArray索引
y =我们需要从其开始的外部数组索引
size = tilemap大小
function makepointfrom(x,y,size)
{
for(var y2= y;y2 < size;y2++)
{
tilemap[y2][x] = 1;
}
console.log(JSON.stringify(tilemap))
}
var tilemap =
[
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]
makepointfrom(2,0,tilemap.length);