JavaScript - for 循环不能正确分配 x 和 y

JavaScript - for loop does not correctly allocate the x and y

本文关键字:分配 for 循环 不能 JavaScript      更新时间:2023-09-26

我在一个简单的JavaScript 2D(画布(游戏中使用A*寻路脚本。我把我的游戏分解为SSCCE。无论如何,我的游戏是 15 列和 10 行。

问题出在哪里?设置图形时,我使用for循环中的简单for循环正确设置了nodes(或者我认为是这样(,以设置一个 15 横板和 10 下板。

因此,当我加载页面时,出现此错误:Uncaught TypeError: Cannot read property '5' of undefined

这是从这一行start = graph.nodes[12][5];(从下面的SSCCE(,如果你改变5(Y(,那么错误中的数字就会改变。

这是我的SSCCE.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>    
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type='text/javascript' src='graphstar.js'></script>
<script type="text/javascript">
    var board;
</script>
<script type='text/javascript' src='astar.js'></script>
<script type="text/javascript">
    $(document).ready(function()
{
        // UP to DOWN - 10 Tiles (Y)
        // LEFT to RIGHT - 15 Tiles (X)
        graph = new Graph([
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
        [1, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 1], 
        [1, 13, 1, 13, 13, 13, 13, 13, 1, 1, 1, 1, 1, 13, 13, 1], 
        [1, 13, 1, 1, 13, 1, 1, 13, 1, 13, 13, 1, 13, 13, 13, 1], 
        [1, 13, 13, 1, 1, 1, 13, 13, 1, 13, 13, 1, 1, 1, 13, 1], 
        [1, 13, 13, 1, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1], 
        [1, 13, 13, 13, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1], 
        [1, 13, 1, 13, 13, 13, 13, 13, 1, 1, 1, 1, 13, 13, 13, 1], 
        [1, 13, 1, 1, 1, 1, 13, 13, 13, 13, 1, 13, 13, 13, 13, 1], 
        [1, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 1], 
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
        ]);
        //Let's do an example test.
        start = graph.nodes[12][5]; // X: 12, Y: 5
        end = graph.nodes[2][4]; // X: 2, Y: 4
        result = astar.search(graph.nodes, start, end);
    });
</script>
</head>
<body>
Loading... pathfinding. Look in Chrome Console/Firefox Firebug for more information.
</body>
</html>

如您所见,我的grid上下10行,横跨16列。但是,我的graph并没有这样解释。

在此处查看整个graphstar.js:http://pastebin.com/xUwghjRR(这是完整包的astar.js:http://pastebin.com/YMBtX66W(

这是它在graphstar.js中布局的地方:新更新

function Graph(grid) {
    var nodes = [];
var row, rowLength, len = grid.length;
for(var y = 0; y < len; ++y){
    row = grid[y];
    rowLength = row.length; // Expected: 16;
    nodes[y] = new Array(len);
    for (var x = 0; x == rowLength - 1; ++x){
            nodes[y][x] = new GraphNode(x, y, row[y]);
    }
}
    this.input = grid;
    this.nodes = nodes;
}

如您所见,for循环是通过查看grid多长时间来完成的。 grid长度应该输出到11这是正确的,因为我们首先循环Y轴。然后通过row应该输出的长度16这是正确的,因为从那时起我们正在做X轴。

因此,当我启动页面时,出现此错误:

所以。。。我想我画的是正确的,但我的XY仍然没有定义。

你在循环中的条件从一开始就是假的,所以它甚至永远不会进入第一个循环,它应该是

for(var y = 0; y < len; ++y){

另外,对GraphNodes的呼吁可能是使用x而不是y

编辑:

代码中还有一些其他的小错误,第二个循环需要大写的 L rowLengthx的顺序和y 在你保存的新节点对象需要切换nodes[y][x] =

编辑2:

工作函数,请注意,为了便于使用,我删除了对 GraphNodes 的函数调用,还有一些控制台.log您需要删除的调用

function Graph(grid) {
    var nodes = [];
    var row, len = grid.length;
    console.log(len);
    for (var y = 0; y < len; ++y) {
        console.log(y);
        row = grid[y];
        console.log(row);
        var rowLength = row.length; // Expected: 16;
        nodes[y] = new Array(len);
        for (var x = 0; x < rowLength; ++x) {
            nodes[y][x] = row[x];
            console.log(nodes);
        }
    }
    this.input = grid;
    this.nodes = nodes;
}

编辑 3:

问题可能是因为所有其他代码都将坐标视为 x,y,而您在此处拥有的循环并存储它们 y,x 如果您像这样更改代码,它应该允许您使用 x,y 保留所有其他代码

function Graph(grid) {
    var nodes = [];
    var row, len = grid.length;
    for (var y = 0; y < len; ++y) {
        row = grid[y];
        var rowLength = row.length; // Expected: 16;
        for (var x = 0; x < rowLength; ++x) {
        if(!nodes[x]){      
                nodes[x] = new Array(len);
        }
            nodes[x][y] = row[x];
        }
    }
    this.input = grid;
    this.nodes = nodes;
}