-3

Basically, I am trying to make a tile-based tower defense game by following a tutorial and so far I have the following code:

private int[] findNextDir(tile s) {
    int[] dir = new int[2];
    tile u = grid.getTile(s.getXPlace(), s.getYPlace() - 1);
    tile r = grid.getTile(s.getXPlace() + 1, s.getYPlace());
    tile d = grid.getTile(s.getXPlace(), s.getYPlace() + 1);
    tile l = grid.getTile(s.getXPlace() - 1, s.getYPlace());

After this point line 54 (tile d's line) returns this (without the code snippets):

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at data.tileGrid.getTile(tileGrid.java:41)

public tile getTile(int xPlace, int yPlace) {
    return map[xPlace][yPlace];

at data.enemy.findNextDir(enemy.java:54)

private int[] findNextDir(tile s) {
    int[] dir = new int[2];
    tile u = grid.getTile(s.getXPlace(), s.getYPlace() - 1);
    tile r = grid.getTile(s.getXPlace() + 1, s.getYPlace());
    tile d = grid.getTile(s.getXPlace(), s.getYPlace() + 1);

at data.enemy.<init>(enemy.java:37)

this.directions = new int[2];
    //xDir
    this.directions[0] = 0;
    //yDir
    this.directions[1] = 0;
    directions = findNextDir(startTile);

at data.boot.<init>(boot.java:31)

enemy e = new enemy(texLoad("enemy"), grid.getTile(10, 10),
        grid, 64, 64, 2);

at data.boot.main(boot.java:49)

public static void main(String[] args) {
    new boot();

The annoying thing is when I take out the '+ 1' it works perfectly except for the lack of the '+ 1'.

SophiaLily
  • 13
  • 3
  • 1
    Have you googled this error and tried debugging it? You're trying to access an element that doesn't exist. And what's the full trace? – Carcigenicate Aug 07 '18 at 18:43
  • 1
    It probably happens when you hit the edge of the map. You have to account for that. – NAMS Aug 07 '18 at 18:43
  • 1
    You're obviously trying to get a tile that doesn't exist. What troubleshooting have you done? What does `grid.getTile()` do? What is `s`? We need to see more code, preferably a [mcve]. – Zephyr Aug 07 '18 at 18:43

2 Answers2

1

I'd take a moment and look at what you're working with. It's an array of set size, which can be found by doing a simple output as a way of debugging.

 System.out.println(arrayname.length());

Why is this important? Look at your error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

What this tells you is that you've exceeded your set array size - when an array is initialized, it's size is set; you can fill each place with new elements or remove them (leaving blanks), but the overall size remains the same. Specifically, in this error, you're attempting to put an 11th element into an array with a size of 10.

This can be caused by many things, but as you've partially discovered, I'd take a look at

tile d = grid.getTile(s.getXPlace(), s.getYPlace() + 1);

What happens here when you're at the edge of the map? When there aren't any more elements after s.getYPlace()? Because you've reached the limit of the map, and arrays are a fixed size, you're attempting to assign a value to an array that doesn't exist - hence the error.

Possible solutions? There are a few, but if you wanted a very specifically targeted one, Why not add a check to your grid? Is adding that +1 actually putting entities where you want it, or is it actually one space off?

0

Ok, I've got it, I was trying to make it abstract and I guess my eclipse was just not liking that. Original Code:

tile u = grid.getTile(s.getXPlace(), s.getYPlace() - 1);
tile r = grid.getTile(s.getXPlace() + 1, s.getYPlace());
tile d = grid.getTile(s.getXPlace(), s.getYPlace() + 1);
tile l = grid.getTile(s.getXPlace() - 1, s.getYPlace());

w/ Abstract

public int getXCoord() {
    return (int) x / 64;
}

public int getYCoord() {
    return (int) y / 64;

This would usually be fine but nope.

    tile u = grid.getTile((int) s.getX() / 64, (int) (s.getY() - 1) / 64);
    tile r = grid.getTile((int) (s.getX() + 1) / 64, (int) s.getY() / 64);
    tile d = grid.getTile((int) s.getX() / 64, (int) (s.getY() + 1) / 64);
    tile l = grid.getTile((int) (s.getX() - 1) / 64, (int) s.getY() / 64);

Without making it abstract and doing all the division here it works perfectly, I haven't the slightest clue why this works but I'm not going to jinx it.

Thanks to everyone who commented or answered! <3

SophiaLily
  • 13
  • 3