嗨,所以我编码游戏,并加载瓷砖,代码将读取地图信息所在的.map文件。但是,它给了我这个错误。
.map文件看起来像这样:这只是一行......
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
发生错误的方法:
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;
}
}
}
发布于 2018-06-25 11:53:14
你正在尝试访问数组中存在的具有X和Y变量的更大索引。在访问数组之前,确保数组包含索引:
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?
}
}
}
或者,只在数组的边界内迭代:
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;
}
}
https://stackoverflow.com/questions/-100005500
复制相似问题