XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

Array Not Working

Last post 11/19/2009 4:19 AM by mtnPhil. 1 replies.
  • 11/19/2009 3:57 AM

    Array Not Working

    I've got this array:
                int myType = 0; 
                int[,] pos = new int[3, 1]; 
                int numb = 0; 
                int formation = 1; 
                switch (formation) 
                { 
                    case 1: 
                        myType = 1; 
                        pos[0, 0] = 16; 
                        pos[0, 1] = -48; 
                        pos[1, 0] = 64; 
                        pos[1, 1] = -32; 
                        pos[2, 0] = 144; 
                        pos[2, 1] = -32; 
                        pos[3, 0] = 144; 
                        pos[3, 1] = -48; 
                        numb = 5; 
                        break
     
                    case 2: 
                        myType = 1; 
                        numb = 4; 
                        break
     
                    case 3: 
                        myType = 1; 
                        numb = 5; 
                        break
                } 
     
                for (int i = 0; i < numb; i++) 
                { 
                    GameObject ship = new GameObject(Content.Load<Texture2D>("en" + myType.ToString())); 
                    ship.X = pos[i, 0]; 
                    ship.Y = pos[i, 1]; 
                    ships.Add(ship); 
                } 
    This code (inside a void function) gets run each time I press space.
    When I press space my game freezes and Visual Studio goes to the line: pos[0, 1] = -48;   and highlights it in yellow, and throws this error:
    "Index was outside the bounds of the array."

    ???
  • 11/19/2009 4:19 AM In reply to

    Re: Array Not Working

    Array indices are 0-based.  For the second index in your array, you are providing 1, but the biggest index allowed is 0 (since the array is of size 1).

    Try new int [4, 2] instead.
Page 1 of 1 (2 items) Previous Next