0

シンプルな JavaScript で A* パスファインディング スクリプトを使用しています。ゲームを SSCCE に分解しました。とにかく、私のゲームは横 15 列、下 10 行です。

パスファインディングは、右端の 5 列のどこかをクリックするまで機能します。したがって、以上のX場合。11このエラーが発生します。クリックした場所の軸はUncaught TypeError: Cannot read property '7' of undefinedどこですか。7Y

これが私の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.jsますastar.js。正常に動作するので心配しないでくださいastar.jsgraphstar.js私の問題があるところです。などが配置されているastar.js場所です。マップがグラフ化されている場所です。nodesgraphstar.js

ここで全体を参照してgraphstar.jsください: http://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;
}

私の の最高は 10 ですが、私の の最高は 10 であることはわかっていXます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 に答える