简体   繁体   English

如何获取由二维数组(JS)制成的网格的坐标?

[英]How to obtain coordinates of a grid made from a two dimensional Array (JS)?

Desired outcome: 期望的结果:

I want to create a 20x20 map by printing 400 images representing the tiles. 我想通过打印代表瓷砖的400张图像来创建20x20的地图。 Then, I want to be able to click those images and receive an alert telling me the corresponding coordinate of that tile. 然后,我希望能够单击这些图像并收到警报,告诉我该图块的相应坐标。

The Problem/Error: 问题/错误:

No matter which Tile I click, the alert message always says x:19,y19, which I'm assuming is the last tile generated only. 无论我单击哪个图块,警报消息始终显示x:19,y19,我假设这是仅生成的最后一个图块。 There is something im doing wrong when it comes to assigning the values, I would guess. 我猜想在分配值时我做错了什么。

How I tried it: 我如何尝试的:

I'm using two for loops to create a 20 by 20 elements grid into a two-dimensional array. 我正在使用两个for循环将20 x 20元素的网格创建为二维数组。 I use this code to make all its elements equal to 0. 我使用此代码使其所有元素等于0。

    for (j = 0; j < 20; j++)
{   
    for (i = 0; i < 20; i++)
    { 
        mapxy[j][i] = 0;
    }
}

After that, I create another two for loops to go through all of these elements and print their values accordingly, one after the other (I don't do it in the first two fors to keep a modular approach): 之后,我创建了另外两个for循环来遍历所有这些元素并依次打印它们的值(我在前两个fors中不这样做以保持模块化方法):

    for (m = 0; m < 20; m++)
{
    for(k = 0; k < 20; k++)
    {
        document.getElementById("map").innerHTML += "<img onclick=\"tileclick(k,m)\" src=\"" + tiletype[mapxy[k][m]] + "\"/>";
    }
    document.getElementById("map").innerHTML += "</br>";
}   

please notice the onclick="tileclick(k,m)" After all this is done, i define what this function actually does (showing the coordinates): 请注意onclick =“ tileclick(k,m)”之后,我定义了此函数的实际作用(显示坐标):

function tileclick(k,m)
{
    alert('x: ' + k + ' y: ' + m);
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks for your time. 谢谢你的时间。

You are using global variable (k and m) so you code is working (meaning no crash) but this part is messy: 您正在使用全局变量(k and m)因此您的代码可以正常工作(这意味着不崩溃),但此部分很混乱:

document.getElementById("map").innerHTML += "<img onclick=\"tileclick(k,m)\" src=\"" + tiletype[mapxy[k][m]] + "\"/>";

You are calling tileclick with k and m variable when you click the image. 单击图像时,将使用km变量调用tileclick At this time, k = 19 and m = 19 (after the loop). 此时, k = 19m = 19 (循环后)。 That's why you get an alert with 19,19 . 这就是为什么您收到19,19 alert的原因。 One solution is to change your line by: 一种解决方案是通过以下方式更改生产线:

document.getElementById("map").innerHTML += "<img onclick=\"tileclick("+k+","+m+")\" src=\"" + tiletype[mapxy[k][m]] + "\"/>";

Following code will work as you want, but I don't recommend it because it isn't optimized. 以下代码可以按您的意愿工作,但是我不建议您这样做,因为它没有经过优化。 Because most of DOM methods including getElementById and innerHTML are high cost functions, you would be better to avoid calling them as much as possible. 由于大多数DOM方法(包括getElementById和innerHTML)都是高成本函数,因此最好避免尽可能多地调用它们。

for (m = 0; m < 20; m++)
{
    for(k = 0; k < 20; k++)
    {
        document.getElementById("map").innerHTML += "<img onclick=\"tileclick("+k+","+m+")\" src=\"" + tiletype[mapxy[k][m]] + "\" />";
    }
    document.getElementById("map").innerHTML += "<br />";
}

Little optimized: 很少优化:

var content = '';

for (m = 0; m < 20; m++)
{
    for(k = 0; k < 20; k++)
    {
        content += '<img onclick="tileclick('+k+','+m+')" src="' + tiletype[mapxy[k][m]] + '" />';
    }
    content += '<br />';
}
document.getElementById("map").innerHTML = content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM