0

I'm making a game but I'm still in the screen-making proces . I've made a spritesheet with black, dark gray, light gray, and white colors and I want to make them bright colors, but I get an ArryIndexOutOfBoundsExeption when I try to initialize the first color.

this is my code for the class where I want help with:

public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1; 

public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH_MASK];
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH_MASK * 4];

public int xOffset = 0;
public int yOffset = 0;

public int width;
public int height;

public SpriteSheet sheet;

public Screen(int width, int height, SpriteSheet sheet){

    this.width = width;
    this.height = height;
    this.sheet = sheet;

    for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++ ){

        colours[i*4+0] = 0xff00ff;
        colours[i*4+1] = 0x00ffff;
        colours[i*4+2] = 0xffff00;
        colours[i*4+3] = 0xffffff;

    }

}

public void Render(int[] pixels, int offset, int row){

    for(int yTile = yOffset >> 3; yTile <=(yOffset + height) >> 3; yTile++){

        int yMin = yTile * 8 - yOffset;
        int yMax = yMin + 8;
        if(yMin < 0) yMin = 0;
        if(yMax > height) yMax = height;

        for(int xTile = xOffset >> 3; xTile <=(xOffset + width) >> 3; xTile++){

            int xMin = xTile * 8 - yOffset;
            int xMax = xMin + 8;
            if(xMin < 0) xMin = 0;
            if(xMax > width) xMax = width;

            int tileIndex = (xTile & (MAP_WIDTH_MASK)) + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH ;

            for(int y = yMin; y < yMax; y++){

                int sheetPixel = ((y + yOffset) & 7) * sheet.width + ((xMin + xOffset) & 7);
                int tilePixel = offset + xMin + y * row;

                for(int x = xMin; x < xMax; x++){

                    int colour = tileIndex * 4 + sheet.pixels[sheetPixel++];

                    pixels[tilePixel++] = colours[colour];

                }

            }

        }

    }

}

If someone knows how to fix the problem so that the exeption is gone, feel free to reply ;)

Tom
  • 39
  • 1
  • 6
  • 1
    Every ArrayIndexOutOfBoundsException is a duplicate. There should almost be an autoclose on such questions. And what is so difficult about a minimal verifiable example? – KevinO Apr 14 '16 at 19:27
  • 1
    Making a simple verification, one receives `Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16128 at (some line number)`. The size of the array is 16128, so clearly there cannot be something placed at [16128]. – KevinO Apr 14 '16 at 19:29
  • Arrays are usually 0 bound so you need to deduct 1 from the index to put it in the right place. – Brody Apr 15 '16 at 01:45

1 Answers1

0

Check the size of the array vs the index you are trying to access.

public int[] colours = new int[MAP_WIDTH * MAP_WIDTH_MASK * 4];

64*63*4 = 16128

Max value of counter is 64*64-1 = 4095

colours[i*4 + 3] == colours[4095*4 + 3] 

4095*4 + 3 = 16,383

16,383 > 16,128, so index out of bounds.

Derek A.
  • 65
  • 5