Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C# arrayoutofbounds
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Sat Sep 13, 2008 3:11 pm   Post subject: C# arrayoutofbounds

I am trying to initialize an array of colours to handle collision detection in an artillery game.

The logic of this code is:
- initialize a 1D array and a 2D to the size of the target map texture, in this case [960, 590] and [960*590]
- Read the color data from the texture into the 1D array
- Transform the 1D array into a 2D array
- Scale the 2D array to twice the size

Everything looks right to me but the compiler is throwing an ArrayOutOfBounds exception on every single line of the scaling algorithm inside the second nested for loop and I have no idea why. Any help is greatly appreciated.


code:

private void ProcessTerrain()
        {
             
            Map.tempMap = new Color[terrainTexture.Width * terrainTexture.Height];
            Map.terrainMap = new Color[terrainTexture.Width, terrainTexture.Height];
            Map.scaledMap = new Color[1920, 1080];
            String error;

            terrainTexture.GetData<Color>(Map.tempMap);
           

            // Translate 1D tempMap array into 2D terrainMap array
           
            for (int i = 0; i < terrainTexture.Width; i++)
            {
                for (int j = 0; j < terrainTexture.Height; j++) {
                    Map.terrainMap[i, j] = Map.tempMap[terrainTexture.Width*j + i];               
                }
            }

            Map.bg = Map.terrainMap[0, 0];

            //Scale to twice the size

            for (int i = 0; i < terrainTexture.Height; i++)
            {
                for (int j = 0; j < terrainTexture.Width; j++)
                {
                        Map.scaledMap[2 * i, 2 * j] = Map.terrainMap[i, j];
                        Map.scaledMap[2 * i + 1, 2 * j] = Map.terrainMap[i, j];
                        Map.scaledMap[2 * i, 2 * j + 1] = Map.terrainMap[i, j];
                        Map.scaledMap[2 * i + 1, 2 * j + 1] = Map.terrainMap[i, j];
                   
                }
            }
        }
Sponsor
Sponsor
Sponsor
sponsor
Stove




PostPosted: Sun Sep 21, 2008 8:47 pm   Post subject: Re: C# arrayoutofbounds

Is the exception thrown when it first enters the 2nd nested loop? or after it starts iterating through it?
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: