我正在尝试为简单的XNA赛车游戏创建碰撞检测方法,我正在使用this tutorial来提取纹理数据。我要做的是检查纹理区域中的任何颜色是否为蓝色(这是我赛道上墙壁的颜色)。但是,我一直在标题中收到错误。任何人都可以向我解释为什么会这样吗?
代码:
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;
}
编辑:我搞定了!
答案 0 :(得分:0)
您的rawData长度不足以接收您尝试使用GetData()方法获取的数据。
更改此行:
Color[] rawData = new Color[width * height];
成:
Color[] rawData = new Color[track.Width * track.Height];
那应该这样做。希望它有所帮助!