我正在嘗試在C#中編寫Unity中的戰術RPG,並且要這樣做,我需要使用尋路,Dijkstra,節點邊緣等。 我試圖將2D區域字符代表我的節點的2D面積邊緣的地圖,這裏的地圖:2D區域中的未知字符
的#
在這裏紀念地圖的邊界和閃避的指數超出範圍的錯誤。
下面是該項目的代碼:
public Nodes[,] GenerateNodes(TextAsset lvl)
{
Nodes[,] nodeArray;
char[,] matrix = GenerateArray(lvl);
nodeArray = new Nodes[matrix.GetLength(0), matrix.GetLength(1)];
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
char type = matrix[row, col];
if (type != '#' && type != '\n')
{
Debug.LogError(type);
Nodes n = new Nodes(row, col, type);
nodeArray[row, col] = n;
findNeighbors(row, col, matrix, nodeArray); //assuming nodes and matrix variables are instance variables
}
else
nodeArray[row, col] = null;
}
}
return nodeArray;
}
public void findNeighbors(int row, int col, char[,] matrix, Nodes[,] nodeArray)
{
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
Debug.LogError(row);
Debug.LogError(col);
if (matrix[row + r, col + c] != '#')
{
nodeArray[row, col].addEdges(nodeArray[row + r, col + c]);
}
}
}
}
的GenerateArray()
方法工作;我之前使用過。問題來自findNeighbors()
。我有這樣的錯誤:
Array Out of range at the row 0 and the columns 19
問題是,我只是有一些#
在這裏,它甚至不應該在這裏。我試圖打印該字符,它不打印任何東西,所以我試圖重寫該行,壓制\n
等,我什麼也沒有得到。
示例文件的行尾是否爲「\ n」?或者他們是「\ r \ n」? –
我在窗戶上,很好的猜測,那是該死的\ r,謝謝! –