0

我正在开发一个 windows phone 7 游戏,我已经将关卡保存在一个文本文件中,我想将它加载到一个二维数组中,但是没有 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))

但它抛出了同样的错误:没有处理这种文件类型的进口商。

做什么?

4

3 回答 3

1

you can do that with isolated storage only just by changing the name of file "myfile1.txt" and change the attribute FileMode.OpenOrCreate if you are not having it already.

于 2012-11-27T16:32:53.617 回答
0

不知道这是否会对您有所帮助,但这就是我将游戏地图加载到我的游戏中的方式(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 }
    }
}
于 2012-12-01T05:34:31.970 回答
0

您应该能够添加文件,将“构建操作”设置为“无”,并将“复制到输出目录”设置为“如果较新则复制”并使用加载它

XDocument.Load(TitleContainer.OpenStream(@"path-to-file.txt"));

如此处所示。

希望这可以帮助!

于 2012-11-26T16:29:05.163 回答