0

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

寻路一直有效,直到您单击最右侧 5 列上的任意位置。所以如果X11或更大。你会得到这个错误。您单击的轴在哪里Uncaught TypeError: Cannot read property '7' of undefined7Y

这是我的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 - y (15 columns across, 10 rows down)
        end = graph.nodes[12][7]; // x - y (15 columns across, 10 rows down)
        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/kx4mw86z(这里是astar.js: http: //pastebin.com/wtN2iF15

这是它的布局graphstar.js

// Creates a Graph class used in the astar search algorithm.
function Graph(grid) {
    var nodes = [];

    var row, rowLength, len = grid.length;

            for (x = 0; x <= 10; x++) {
             row = grid[x];
             nodes[x] = new Array(15);
                for (y = 0; y <= 15; y++) {
                   nodes[x][y] = new GraphNode(x, y, row[y]); 
                }
            }

    this.input = grid;
    this.nodes = nodes;
}

我知道我的X最高值是 10。但我试着弄乱它.. 我会出错。有时没有错误,页面会卡住。15Y

帮助?

新的图形格式:

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

如果我正确理解这一切,我认为您只是将索引倒退。

graph.nodes[12][7]

graph.nodes[12]未定义,因为 中只有 11 个元素nodes

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

    nodes[x] = new Array(15);  // x only goes up to 10

编辑:

这条评论说明了一切:

// UP to DOWN - 10 Tiles (Y)
// LEFT to RIGHT - 15 Tiles (X)

这是倒退。 你没有 15 x 和 10 y,你有 10 x 和 15 y。

于 2012-05-05T03:25:27.677 回答