JavaScript - for loops on the x and y axes

I am using an A * pathfinding script in a simple JavaScript 2D (canvas) game. I broke my game before SSCCE . Anyway, my game consists of 15 columns and 10 rows down.

What is the problem? . When creating a graph, nodes are configured only 11 times and 10 times up and down. The axis Xshould be up 15, when, apparently, it is set only to grid.length, which is equal 11. Good, so there is my problem. Mine gridis the refund arrayincluded in SSCCE.

Here is mine 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 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>

, grid - 11, , 16. graph .

graphstar.js : http://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 15 Y - 10?

+3
1

EDIT: , .

, 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;
    }
}
+1

All Articles