単純なJavaScript2D(キャンバス)ゲームでA*パスファインディングスクリプトを使用しています。ゲームをSSCCEに分解しました。とにかく、私のゲームは横に15列、下に10行です。
問題は私が得るエラーです。以下に、ノード間のパスを開始および終了する方法があります。のエラーメッセージは次のとおりUncaught TypeError: Cannot set property '0' of undefined
ですline 15
。15行目は2番目のループnodes[x][y] = new GraphNode(x, y, row[x]);
の間にあります。for
これが私の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>
ご覧のとおり、私のゲームはjQuery
です。ともありgraphstar.js
ますastar.js
。正常に動作するので心配しないでくださいastar.js
。graphstar.js
私の問題はどこにありますか。astar.js
とnodes
そのようなものが配置されている場所です。graphstar.js
マップがグラフ化される場所です。
ここで全体を参照してください:http graphstar.js
://pastebin.com/5AYRreip(ここにあります:http astar.js
://pastebin.com/ee6PMzc3)
これはそれがレイアウトされている場所ですgraphstar.js
:
function Graph(grid) {
var nodes = [];
var row, rowLength, len = grid.length;
for (y = 0; y <= 15; y++) {
row = grid[y];
nodes[y] = new Array(15);
for (x = 0; x <= 10; x++) {
nodes[x][y] = new GraphNode(x, y, row[x]);
}
}
this.input = grid;
this.nodes = nodes;
}
ご覧のとおり、以下の場合が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]);
}
}