2012-09-08 30 views
0

我想讀取文本文件以構建地圖。在XNA中逐行讀取文本文件以在XNA中創建地圖

例如,我有這樣的地圖:

[email protected] 
[email protected] 
[email protected] 
000000000 
000000000 
000000000 
000000000 

我知道我應該使用這樣的:現在

StreamReader reader = new StreamReader([email protected]"/TestMap.MMAP"); 
string line = reader.ReadToEnd(); 
reader.Close(); 

,例如,我想讀第2行字符 「@」。我怎樣才能做到這一點?

請幫幫我。


解決:

謝謝(@LB和@ user861114),最後我的問題解決了:

string[,] item = new string[9, 7]; 
string[] line = File.ReadAllLines(Application.StartupPath + @"/TestMap.MMAP"); 
for (int j = 0; j < 7; j++) 
{ 
    for (int i = 0; i < 9; i++) 
    { 
     item[i, j] = line[j].Substring(i, 1); 
     Console.WriteLine(i + " " + j + "=" + item[i, j]); 
    } 
} 

回答

1
string[] lines = File.ReadAllLines(your path); 

那麼你可以訪問

char ch = lines[1][1]; //second line's second char 
1

我覺得它有點簡單:

string[] strs = string.split(myString, "\n"); // split to array of string by delimiter  endline 
char[] chars = strs[1].ToCharArray(); // you can get second char "@"