JavaScript-for循环抛出未定义的变量。(SSCCE提供)

JavaScript-for循环抛出未定义的变量。(SSCCE提供),javascript,jquery,html,a-star,Javascript,Jquery,Html,A Star,我在一个简单的JavaScript中使用一个a*寻路脚本。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行 在您单击最右边5列上的任意位置之前,路径查找都会一直工作。因此,如果X为11或更大。您将得到这个错误未捕获类型错误:无法读取未定义的属性“7”,其中7是单击位置的Y轴 这是我的SSCCE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/

我在一个简单的JavaScript中使用一个a*寻路脚本。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行

在您单击最右边5列上的任意位置之前,路径查找都会一直工作。因此,如果
X
11
或更大。您将得到这个错误<代码>未捕获类型错误:无法读取未定义的属性“7”,其中
7
是单击位置的
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[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>
我知道我的
X
最高是
15
,而我的
Y
最高是10。但是我试着去摆弄它。。我会犯错误。有时没有错误,页面就会卡住

帮忙

新图形格式:

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]);
    }
}

for(y=0;y

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

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

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

这是向后的。你没有15 x和10 y,你有10 x和15 y。

图形。节点[12][7]
表示12向下,7横向,根据你的数组布局。但是没有第12行。也许你的意思是
[7][12]
?不,因为那样会混淆Y和X……这是我的观点,你把它们混淆了。我明白了。我试图改变它们,但仍然得到相同的错误。只是这次……这个数字是0。对。我明白了,我试着把它改成10到15……但是我得到了相同的错误。我不知道还能做什么?只是改变循环是行不通的为了提供帮助。您传递给构造函数的数组也只有11个元素。您混淆了x和y。我的构造函数向下有10行…我如何将其设置为15行?我的意思是,如何在不扩展行的情况下扩展元素?我不确定如何解释它,我更新了我的答案,以使其更加清晰。您有您的xy向后,你正在向后使用你的数组。
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)