JavaScript-用于x轴和y轴上的循环
我正在一个简单的JavaScript 2D(画布)游戏中使用。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行 问题出在哪里?设置图表时,节点只设置了11次,上下10次。当JavaScript-用于x轴和y轴上的循环,javascript,jquery,html,path-finding,a-star,Javascript,Jquery,Html,Path Finding,A Star,我正在一个简单的JavaScript 2D(画布)游戏中使用。我把我的比赛打成了一场比赛。无论如何,我的游戏是15列,10行 问题出在哪里?设置图表时,节点只设置了11次,上下10次。当X轴仅设置为grid.length,即11时,它的最大值应为15。好的,这就是我的问题。我的网格是SSCCE中包含的返回数组 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
X
轴仅设置为grid.length
,即11
时,它的最大值应为15
。好的,这就是我的问题。我的网格
是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>
这是我的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>
如您所见,for的循环只进入网格。length
(len
)是11
,因为它是11
行。但那是X
轴。我的X
轴的映射跨越15列,而Y
轴向下是16行
现在你说为什么不切换轴?我试过了。例如,X
和Y
正在切换。但是我得到的只是uncaughttypeerror:无法在第24行中设置未定义的
的属性“0”,它是节点[x][y]=新图形节点(x,y,行[y])代码>
我怎样才能使图表相应地设置为X
的最高轴是15
,Y
的最高轴是10
?编辑:曲解了你刚才说的话
看起来您只需要改变使用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;
}
}
运行此命令时出现以下错误<代码>未捕获类型错误:无法读取未定义的属性“8”,其中8
是我在Y
轴上单击的数字。