![](/img/trans.png)
[英]Windows Phone 8 - reading and writing in an existing txt file in the project
[英]windows phone, reading level from txt file
我正在使用Windows Phone 7游戲,並且已將關卡保存在文本文件中,我想將其加載到2D數組中,但是沒有用於txt文件的內容導入器,因此我使用了隔離存儲管理器
protected void read_lvl()
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
while (!reader.EndOfStream)
{
//for each row
for (int i = 0; i < rows; i++)
{
//read in the line
string myLine = reader.ReadLine();
//take out the commas
string[] row = myLine.Split(',');
//convert to string to ints
//and feed back into array
int[] nRow = new int[row.Length];
for(int r=0; r<columns;r++){
nRow[r] =Convert.ToInt32(row[r]);
myreadArray[i, r] = nRow[r];
}
}
}
}
}
這對於加載保存的游戲狀態等很好,但是我想在多個.txt
文件中具有多個級別,並嘗試使用它代替:
//stream from file
Stream stream = TitleContainer.OpenStream("myFile.txt");
//make a stream reader from the stream
using (StreamReader sreader = new StreamReader(stream))
但是它拋出了同樣的錯誤:沒有導入程序可以處理此文件類型。
做什么?
您可以僅通過更改文件“ myfile1.txt”的名稱並使用屬性FileMode.OpenOrCreate來更改隔離存儲(如果還沒有的話)。
您應該能夠添加文件,將“構建操作”設置為“無”,並將“復制到輸出目錄”設置為“如果更新則復制”,然后使用
XDocument.Load(TitleContainer.OpenStream(@"path-to-file.txt"));
如這里所示。
希望這可以幫助!
不知道這是否對您有幫助,但這是我將游戲地圖加載到游戲中的方式(x和y是我的網格坐標):
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("View.Maps.Breakout_" + _breakoutSelection + ".txt");
using (var streamReader = new StreamReader(stream))
{
int y = 0;
while (!streamReader.EndOfStream)
{
string data = streamReader.ReadLine();
int x = 0;
foreach (var mapSquare in data)
{ code here }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.