我正在尝试为一个简单的 XNA 赛车游戏创建碰撞检测方法,为此我正在使用本教程来了解如何提取纹理数据。我要做的是检查纹理区域中的颜色是否为蓝色(这是我赛道上墙壁的颜色)。但是,我不断收到标题中的错误。谁能向我解释为什么会这样?
代码:
public bool Collision()
{
int width = arrow.Width; //arrow is the name of my "car" texture (it's an arrow)
int height = arrow.Height;
int xr = (int)x; // x is the x position of my arrow
int yr = (int)y; // y is the y position of my arrow
Color[] rawData = new Color[width * height];
Rectangle extractRegion = new Rectangle(xr, yr, width, height);
track.GetData<Color>(0, extractRegion, rawData, 0, width * height); //error occurs here
Color[,] rawDataAsGrid = new Color[height, width];
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
rawDataAsGrid[row, column] = rawData[row * width + column];
}
}
for (int x1 = (int)x; x1 < width; x1++)
{
for (int y1 = (int)y; y1 < height; y1++)
{
if (rawDataAsGrid[x1, y1] == Color.Blue)
{
return true;
}
}
}
return false;
}
编辑:我让它工作了!