-3

Accordin to the debug screen the error is in:

1.Line 61: (Class Sprite)

private void load() {
    for (int y = 0; y < SIZE; y++) {
        for (int x = 0; x < SIZE; x++) {
            pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];      //Here is the error.
        }
    }
}

2.Line 43: (Class Sprite)

public Sprite(int size, int x, int y, SpriteSheet sheet) {
    SIZE = size;
    pixels = new int[SIZE * SIZE];
    this.x = x * size;
    this.y = y * size;
    this.sheet = sheet;
    load();                                     //Here is the error.
}

3. Line 16: (Class Sprite)

public static Sprite spawn_grass = new Sprite(16, 0, 3, SpriteSheet.spawn_level);   //Here is the error.
public static Sprite spawn_stone = new Sprite(16, 0, 2, SpriteSheet.spawn_level);
public static Sprite spawn_water = new Sprite(16, 1, 3, SpriteSheet.spawn_level);
public static Sprite spawn_wall = new Sprite(16, 1, 1, SpriteSheet.spawn_level);
public static Sprite spawn_wall2 = new Sprite(16, 1, 2, SpriteSheet.spawn_level);
public static Sprite spawn_floor = new Sprite(16, 0, 0, SpriteSheet.spawn_level);
  1. Line 23: (Class Player)

    public Player(int x, int y, Keyboard input) {
    this.x = x;
    this.y = y;
    this.input = input;
    sprite = Sprite.player_forward;          //Here is the error.
    

    }

5.Line 46: (Class Game)

public Game() {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

    screen = new Screen(width, height);
    frame = new JFrame();
    key = new Keyboard();
    level = Level.spawn;
    player = new Player(16 * 6, 16 * 4, key);       //Here is the error.

    addKeyListener(key);
}

What can i do? Help!

1 Answers1

0

Instead of arrays use an Array List because since load() is only in the constructor (only called once as far as I can see) you can append the elements instead of finding the correct index for an array. As a result, if you append a list you can never get an out of bounds error.

Array List Code:

ArrayList<Integer> list = new ArrayList<>(); //init list

list.add(); //put same thing you would with array in ()

If the error still persist then the problem is the sheet.pixels[] however I can not see that code so if that is the problem I can't help you until I see where that was initialized

JGerulskis
  • 800
  • 2
  • 10
  • 24