首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复2D游戏中的“system.indexoutofrangeException”?C#Win窗体?

如何修复2D游戏中的“system.indexoutofrangeException”?C#Win窗体?
EN

Stack Overflow用户
提问于 2018-06-25 02:14:59
回答 1查看 0关注 0票数 0

标题

嗨,所以我编码游戏,并加载瓷砖,代码将读取地图信息所在的.map文件。但是,它给了我这个错误。

.map文件看起来像这样:这只是一行......

代码语言:javascript
复制
48:48:00:06 48:48:00:06 48:00:00:07 1F:00:00:07 22:00:00:07 22:00:00:07 20:00:00:06 22:00:00:06 22:00:00:07 1F:00:00:06 82:00:00:06 87:52:00:06 81:52:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06

发生错误的方法:

代码语言:javascript
复制
public void _SetTiles(string mapFile)
{
    int Y = 0, X = 0;
    string[] Parameter = new string[4];
    string[] TilesInfo = File.ReadAllLines(mapFile);
    for (int y = 0; y < TilesInfo.Count(); y++)
    {
        string[] WordsInfo = TilesInfo[y].Split(' ');
        Y = TilesInfo.Count();
        for (int x = 0; x < WordsInfo.Count(); x++)
        {
            X = WordsInfo.Count();
            Parameter = WordsInfo[x].Split(':');
        }
    }
    Members.T_XCOUNT = X;
    Members.T_YCOUNT = Y;
    Members.WORLD = new Panel { Location = new Point(0, 0), Size = new Size(X * 30, Y * 30), Visible = true, BackColor = Color.Gray };
    Members.VIEW.Controls.Add(Members.WORLD);
    Members.WORLD.BringToFront();
    for (int y = 0; y < Y; y++)
    {
        for (int x = 0; x < X; x++)
        {
            Members.TILE[x, y].FORE_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[0] + DEFAULT_SPRITE_EXT); //ERROR IS HERE <<<<<<<<<<<<<<
            Members.TILE[x, y].BACK_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[1] + DEFAULT_SPRITE_EXT);
            if (Parameter[3] == "06")
                Members.TILE[x, y].COLLSION = true;
            else
                Members.TILE[x, y].COLLSION = false;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-25 11:53:14

你正在尝试访问数组中存在的具有X和Y变量的更大索引。在访问数组之前,确保数组包含索引:

代码语言:txt
复制
for (int y = 0; y < Y; y++)
{
   for (int x = 0; x < X; x++)
   {
       if (Members.TILE.GetLength(0) > x && Members.TILE.GetLength(1) > y)
       {
            Members.TILE[x, y].FORE_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[0] + DEFAULT_SPRITE_EXT); //ERROR IS HERE <<<<<<<<<<<<<<

            Members.TILE[x, y].BACK_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[1] + DEFAULT_SPRITE_EXT);

           if (Parameter[3] == "06")
               Members.TILE[x, y].COLLSION = true;
           else
               Members.TILE[x, y].COLLSION = false;
       }
       else
       {
          //What will you do when the array is less than the value of x or y?
       }
    }
}

或者,只在数组的边界内迭代:

代码语言:txt
复制
for (int y = 0; y < Y && y < Members.TILE.GetLength(1); y++)
{
   for (int x = 0; x < X && x < Members.TILE.GetLength(0); x++)
   {
        Members.TILE[x, y].FORE_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[0] + DEFAULT_SPRITE_EXT); //ERROR IS HERE <<<<<<<<<<<<<<

        Members.TILE[x, y].BACK_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[1] + DEFAULT_SPRITE_EXT);

        if (Parameter[3] == "06")
           Members.TILE[x, y].COLLSION = true;
        else
           Members.TILE[x, y].COLLSION = false;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档