3

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

问题是什么?设置图表,节点只设置了 11 次横向和 10 次上下。X轴应该是 up to when15看起来它只设置到grid.lengthwhich is 11。好的,所以这是我的问题。Mygridarray包含在SSCCE.

这是我的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 - 11 Tiles (Y)
        // LEFT to RIGHT - 16 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>

如您所见,我grid11上下行和16列。然而,我graph并没有那样解释它。

在这里查看全部内容:http graphstar.js: //pastebin.com/KfS9ALFq(这里是astar.js完整包: http: //pastebin.com/8WyWnTpQ

这是它的布局graphstar.js

function Graph(grid) {
    var nodes = [];

    var row, rowLength, len = grid.length;
    console.log("Length:" + len);
    for (var x = 0; x < len; ++x) {
        row = grid[x];
        rowLength = row.length;
        nodes[x] = new Array(rowLength);
        for (var y = 0; y < rowLength; ++y) {
            nodes[x][y] = new GraphNode(x, y, row[y]);
        }
    }

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

如您所见,for循环仅转到grid.length( len) ,11因为它是11行向下的。但那是X轴。我的X轴地图15跨列,Y轴向16下行。

现在你说...你为什么不切换轴呢?我试过了。喜欢,X并被Y切换。但我得到的Uncaught TypeError: Cannot set property '0' of undefined只是line 24其中nodes[x][y] = new GraphNode(x, y, row[y]);

我怎样才能使图表相应地设置为X最高轴是15Y最高轴是10

4

1 回答 1

1

编辑:误解了你在说什么。

似乎你只需要改变你用 for 循环迭代的方式,这样你就可以向下一个并穿过它,向下一个并穿过它

var row, rowLength, len = grid.length;
for(y = 0; y == len - 1; ++y) {
    row = grid[x];
    rowLength = row.length; // Expected: 16;
    nodes[y] = new Array(len);
    for (var x = 0; x == rowlength - 1; ++x){

            //figure out your nodes[y][x] here;
    }
}
于 2012-05-06T04:27:48.137 回答