矩形阵列的索引超出范围?

时间:2012-09-10 03:40:38

标签: c# arrays xna collision-detection rectangles

我有一个问题,就是为列表中的每个值分配单独的矩形。为了试试这个,我首先尝试给一个值为5的矩形。

snakeBodyRectangleArray[5] = new Rectangle((int)snakeBodyArray[5].X, (int)snakeBodyArray[5].Y, textureSnakeBody.Width, textureSnakeBody.Height);
// above is in update 
protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        graphics.PreferredBackBufferWidth = 500;
        graphics.PreferredBackBufferHeight = 500;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();
        Window.Title = "Curvy Snake";

        snakeBodyArray = new List<Vector2>();
        snakeBodyRectangleArray = new List<Rectangle>();

        bodyTimer.Elapsed += new ElapsedEventHandler(updateBodyPositions);
        bodyTimer.Interval = 120;
        bodyTimer.Enabled = true;

        base.Initialize();
    }

如果您需要更多代码,请询问。

提前致谢

3 个答案:

答案 0 :(得分:1)

看起来您的数组是5个元素,或者更确切地说是number of element is <6并且您正在尝试访问超出snakeBodyRectangleArray范围的元素

答案 1 :(得分:0)

snakeBodyRectangleArray是如何声明的? 它的大小是6还是更高?

答案 2 :(得分:0)

我用过

snakeBodyRectangleArray[bodyNumber] = new Rectangle((int)snakeBodyArray[bodyNumber].X, (int)snakeBodyArray[bodyNumber].Y, textureSnakeBody.Width, textureSnakeBody.Height);

并将其置于私人空白处。