1

I have this problem with assigning individual rectangles to each value in an list. To try this out I first tried to give a rectangle to value 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();
    }

If you need more code, just ask.

thanks in advance

4

3 回答 3

1

It looks like that your array is a 5 element one or more precisely it's number of element is <6 and you are trying to access an element which is out of the range of snakeBodyRectangleArray

于 2012-09-10T03:50:50.890 回答
0

How is snakeBodyRectangleArray declared? Does it have a size of 6 or higher?

于 2012-09-10T03:43:54.110 回答
0

I used

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

and put it into a private void.

于 2013-05-24T00:03:46.467 回答