XNA C#.txt文件平铺引擎

XNA C#.txt文件平铺引擎,c#,xna,C#,Xna,在XNA中使用.txt文件时,我不是很有经验,因此我需要一些帮助 我试图让一个平铺引擎根据.txt文档中的数字来读取和放置信息。它看起来像这样: 1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 etc 现在,该文档在X中包含50个字符(25个字符,不带[,]),在Y中有15行。问题是它只读取Y中的第一行,而不读取Y中剩余的14行,这导致它崩溃

在XNA中使用.txt文件时,我不是很有经验,因此我需要一些帮助

我试图让一个平铺引擎根据.txt文档中的数字来读取和放置信息。它看起来像这样:

1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 etc
现在,该文档在X中包含50个字符(25个字符,不带[,]),在Y中有15行。问题是它只读取Y中的第一行,而不读取Y中剩余的14行,这导致它崩溃

代码如下:

public void LoadMap(string MapName)
    {

        using (StreamReader streamReader = new StreamReader("Content/" + MapName + ".txt"))
        {

            do
            {
                string line = streamReader.ReadLine();

                string[] numbers = line.Split(',');

                int temp = 0;

                    for (int y = 0; y < loopY; y++)
                    {
                        for (int x = 0; x < loopX; x++)
                        {

                            Blocks[y, x] = new Block();

                            //Crashes here when temp reaches 25
                            if (int.Parse(numbers[temp]) == 1)
                                Blocks[y, x].color = Color.Blue;

                            else if (int.Parse(numbers[temp]) == 2)
                                Blocks[y, x].color = Color.Violet;

                            else
                                Blocks[y, x].color = Color.White;

                            temp++;
                        }

                    }

            } while (!streamReader.EndOfStream);


        }

    }
public void LoadMap(字符串MapName)
{
使用(StreamReader StreamReader=newstreamreader(“Content/”+MapName+“.txt”))
{
做
{
string line=streamReader.ReadLine();
string[]number=line.Split(',');
内部温度=0;
for(int y=0;y
根据您对我问题的回答:


“索引超出了数组的边界。”是的,我检查了它loopX是25,数字是25

numbers[]
为零索引,因此上界为
numbers[24]
。如果
loopX
为25,则是,您将看到异常

您正在循环loopX,然后循环并每次递增
temp
。在每次loopX迭代后,您需要将
temp
设置回
0
,或者只使用
numbers
数组而不是循环值

我建议将循环改为使用
数字

for (int x = 0; x < numbers.Length; x++)
编辑:这就是我想要解释的:

using (StreamReader streamReader = new StreamReader("Content/" + MapName + ".txt"))
{
    int y = 0;

    do
    {
        string line = streamReader.ReadLine();

        string[] numbers = line.Split(',');

        for (int x = 0; x < numbers.Length; x++)
        {
            Blocks[y, x] = new Block();

            if (int.Parse(numbers[x]) == 1)
                Blocks[y, x].color = Color.Blue;

            else if (int.Parse(numbers[x]) == 2)
                Blocks[y, x].color = Color.Violet;

            else
                Blocks[y, x].color = Color.White;
         }

           y++;

        } while (!streamReader.EndOfStream);


    }
使用(StreamReader StreamReader=newstreamreader(“Content/”+MapName+“.txt”))
{
int y=0;
做
{
string line=streamReader.ReadLine();
string[]number=line.Split(',');
对于(int x=0;x
根据您对我问题的回答:


“索引超出了数组的边界。”是的,我检查了它loopX是25,数字是25

numbers[]
为零索引,因此上界为
numbers[24]
。如果
loopX
为25,则是,您将看到异常

您正在循环loopX,然后循环并每次递增
temp
。在每次loopX迭代后,您需要将
temp
设置回
0
,或者只使用
numbers
数组而不是循环值

我建议将循环改为使用
数字

for (int x = 0; x < numbers.Length; x++)
编辑:这就是我想要解释的:

using (StreamReader streamReader = new StreamReader("Content/" + MapName + ".txt"))
{
    int y = 0;

    do
    {
        string line = streamReader.ReadLine();

        string[] numbers = line.Split(',');

        for (int x = 0; x < numbers.Length; x++)
        {
            Blocks[y, x] = new Block();

            if (int.Parse(numbers[x]) == 1)
                Blocks[y, x].color = Color.Blue;

            else if (int.Parse(numbers[x]) == 2)
                Blocks[y, x].color = Color.Violet;

            else
                Blocks[y, x].color = Color.White;
         }

           y++;

        } while (!streamReader.EndOfStream);


    }
使用(StreamReader StreamReader=newstreamreader(“Content/”+MapName+“.txt”))
{
int y=0;
做
{
string line=streamReader.ReadLine();
string[]number=line.Split(',');
对于(int x=0;x
我在您的代码中看到以下内容,至少可以说是可疑的:


  • 如果您的temp>=numbers.Length,那么您将得到“索引超出了数组的边界”

  • 如果在阅读第一行之后发生这种情况,当然您不会再阅读更多的行,因为您得到了一个异常,它将中断

  • 你正在用每一条新行覆盖你的块:如果你读第一行并进入x和y循环,在x和y的第一次迭代中,它们都将是0。所以你写
    块[0,0]=newblock()
    。阅读下一行之后,再次进入x和y循环。它们从0开始,因此在处理第二行时,在x和y的第一次迭代中,您将写入
    块[0,0]=new Block()
    。这实际上将覆盖您在处理第一行期间在索引[0,0]上编写的块。同样的问题也存在于x和y的所有其他组合中


  • 调试、单步执行和监视变量。这将帮助您了解正在发生的事情。

    我在您的代码中看到以下内容,至少可以说是可疑的:


  • 如果您的temp>=numbers.Length,那么您将得到“索引超出了数组的边界”

  • 如果在阅读第一行之后发生这种情况,当然您不会再阅读更多的行,因为您得到了一个异常,它将中断

  • 你正在用每一条新行覆盖你的块:如果你读第一行并进入x和y循环,在x和y的第一次迭代中,它们都将是0。所以你写
    块[0,0]=newB