【发布时间】:2013-11-18 14:45:59
【问题描述】:
这里是代码, 我注意到 Getlength 方法返回 0 但我认为它是正常的,因为 for 循环不会超过 0.. 我已经写了
int yy = tile.GetLength(1);
它会抛出与下面的代码相同的异常:
TileData[][] tile = GameMain.Level.getCollisionTiles();
Rectangle tileRectangle;
for(int x = 0; x <= tile.GetLength(0); x++)
{
for (int y = 0; y <= tile.GetLength(1); y++) //EXCEPTION THROWN HERE AT GETLENGTH !!!!
{
tileRectangle = tile[x][y].Target;
if (tileRectangle.Contains(m_hitbox)) //Si il y a collision
{
if ((m_hitbox.X + m_hitbox.Width) > tileRectangle.X) //si le joueur percute par la gauche
{
m_hitbox.X--;
}
else if (m_hitbox.X < (tileRectangle.X + tileRectangle.Width)) //Droite
{
m_hitbox.X++;
}
if ((m_hitbox.Y + m_hitbox.Height) > tileRectangle.Y) //si le joueur percute par le haut
{
m_hitbox.Y--;
}
else if (m_hitbox.Y < (tileRectangle.Y + tileRectangle.Height)) //Bas
{
m_hitbox.Y++;
}
}
}
}
编辑:我通过从地图对象获取图块信息使其工作,但现在它抛出 NullReferenceException
TileData[][] tile = GameMain.Level.getCollisionTiles();
int xMax = GameMain.Level.getMapHeight();
int yMax = GameMain.Level.getMapWidth();
for (int x = 0; x <= xMax; x++)
{
for (int y = 0; y <= yMax; y++)
{
Rectangle tileRectangle = tile[x][y].Target; //THIS LINE FAILS !!!!
if (tileRectangle.Contains(m_hitbox)) //Si il y a collision
{
if ((m_hitbox.X + m_hitbox.Width) > tileRectangle.X) //si le joueur percute par la gauche
{
m_hitbox.X--;
}
else if (m_hitbox.X < (tileRectangle.X + tileRectangle.Width)) //Droite
{
m_hitbox.X++;
}
if ((m_hitbox.Y + m_hitbox.Height) > tileRectangle.Y) //si le joueur percute par le haut
{
m_hitbox.Y--;
}
else if (m_hitbox.Y < (tileRectangle.Y + tileRectangle.Height)) //Bas
{
m_hitbox.Y++;
}
}
}
}
很抱歉在同一主题中问你这个问题,但我认为你可以帮助我快速解决它。
【问题讨论】:
-
它不是二维数组。 TileData[][] 不是 TileData[,]
标签: c# arrays multidimensional-array