2

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

问题是我得到的错误。下面我有一种方法来开始和结束节点之间的路径。Uncaught TypeError: Cannot set property '0' of undefined这是关于的错误消息line 15。第 15nodes[x][y] = new GraphNode(x, y, row[x]);行在第二个for循环之间。

这是我的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[1][2]; // X: 1, Y: 2
        end = graph.nodes[12][7]; // X: 12, Y: 7
        result = astar.search(graph.nodes, start, end);
    });
</script>
</head>
<body>
Loading... pathfinding. Look in Chrome Console/Firefox Firebug for more information.
</body>
</html>

如您所见,我的游戏是jQuery. 还有graphstar.jsastar.js。不要担心,astar.js因为它工作正常。graphstar.js是我的问题所在。astar.jsnodes布置的地方。graphstar.js是地图绘制的地方。

在这里查看全部内容:http graphstar.js: //pastebin.com/5AYRreip(这里是astar.js: http: //pastebin.com/ee6PMzc3

这是它的布局graphstar.js

function Graph(grid) {
    var nodes = [];
    var row, rowLength, len = grid.length;
    for (y = 0; y <= 15; y++) {
        row = grid[y];
        nodes[y] = new Array(15);
        for (x = 0; x <= 10; x++) {
            nodes[x][y] = new GraphNode(x, y, row[x]);
        }
    }
    this.input = grid;
    this.nodes = nodes;
}

所以,正如你所看到的......Y可以是10或更低。X可以是15或更低。但是,我收到此错误。

我在哪里输入end = graph.nodes[12][7]; // X: 12, Y: 7 应该工作,因为它在界限XY......但是我什至在首先设置它时遇到了麻烦。

为什么它是未定义的?

更新新

for (y = 0; y <= 10; y++) {

    row = grid[y];
    nodes[y] = new Array(15);

    for (x = 0; x <= 15; x++) {

        console.log("X: " + x + " Y: " + y);
        //console.log("Row: " + row[x]);
        nodes[x][y] = new GraphNode(x, y, row[x]);
    }
}
4

1 回答 1

2

你把你的轴弄混了。

function Graph(grid) {
    var nodes = [];
    var row, rowLength, len = grid.length;
    for (y = 0; y <= 15; y++) {
        row = grid[y];
        // Here, you're indexing into `nodes` using `y`, so you're creating
        // an array at `nodes[0]` through `nodes[15]`
        nodes[y] = new Array(15);
        for (x = 0; x <= 10; x++) {
            // But here you're index into `nodes` using `x` in the first dimension,
            // so you're filling in `nodes[0][0]` through `nodes[10][15]`, not
            // `nodes[15][10]`.
            nodes[x][y] = new GraphNode(x, y, row[x]);
        }
    }
    this.input = grid;
    this.nodes = nodes;
}

因此,当您检索 时nodes[12],您会得到,undefined因为从来没有为该索引分配值。然后你尝试索引它,并得到错误。

您需要确保始终使用一个坐标(或 ,您喜欢的任何一个)索引到外部数组,并使用另一个坐标(或xy您喜欢的任何一个)索引到内部数组。yx

于 2012-05-05T08:34:21.250 回答