JavaScript-未捕获类型错误:无法设置属性';0';未定义的

JavaScript-未捕获类型错误:无法设置属性';0';未定义的,javascript,jquery,html,a-star,Javascript,Jquery,Html,A Star,我正在一个简单的JavaScript 2D(画布)游戏中使用。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行 问题是我犯的错误。下面我有一个方法来开始和结束节点之间的路径。以下是错误消息未捕获类型错误:无法在第15行设置未定义的属性“0”。第15行是节点[x][y]=新的图形节点(x,y,行[x]) 这是我的SSCCE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.

我正在一个简单的JavaScript 2D(画布)游戏中使用。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行

问题是我犯的错误。下面我有一个方法来开始和结束节点之间的路径。以下是错误消息
未捕获类型错误:无法在
第15行设置未定义的属性“0”。第15行是
节点[x][y]=新的图形节点(x,y,行[x])循环之间的code>

这是我的
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>
所以,正如你所看到的<代码>Y
可以是
10
或更低
X
可以是
15
或更低。然而,我得到了这个错误

其中,当我输入
end=graph.nodes[12][7];//X:12,Y:7
应该可以工作,因为它在
X
Y
范围内。。。然而,我有困难,甚至设置在第一位

为什么它没有定义

更新新内容

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你把你的轴搞混了

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;
}
函数图(网格){
var节点=[];
变量行,行长,len=grid.length;

对于(y=0;y我明白了。我的问题是,如果我尝试索引15列
y
和10行
X
,我会得到那个错误。@weka:很好,很高兴这有帮助。是的,我看到了问题。目前仍在尝试解决它。我的构造函数向下有10行…我如何将其设为15行?我的意思是,我如何在不扩展行的情况下扩展元素?@wekaka:我不能告诉你如何修复它,因为我不知道你想要实现什么;修复它取决于你。我只能告诉你出了什么问题。你传递到
图形
中的是一个11x16的网格。在
图形
中,
网格。长度
是11,
网格[n]。长度
是16(一致性;在JavaScript中,不需要一致性,但在您的情况下是一致的)。但有一半的时间,您将其视为16x11而不是11x16。您需要一致性。一致性的形式取决于您。
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;
}