MouseListener ArrayOutOfBoundsException

Hello JGO,

Recently, I have been working on a game which involves moving a spite towards another sprite using a MouseListener, then hiding the first sprite. So far, I’ve tried doing this:


// mouseX and mouseY are the mouse's X and Y.
if (level.getMap(mouseX, mouseY).equals("1")) { // "1" stands for the 2nd sprite
			c.goAway();
}

The getMap() method takes an x and a y value. This is what happens when I run the program.

Can anyone help me with this error?

Thanks,

DarkCart

Hi

I think I have an idea what your issue may be, but a pastebin would be easier to read than a gif.

EDIT: The issue is definitely how you get the Y value on line 46. Please post the code to get that value.

CopyableCougar4

Your map array is only 20 long. The mouse coordinates are in pixels, which means that as soon as you move your mouse past 20 pixels, you will get a crash. I think that you are trying to get the tile that the mouse is over. If this is the case, then divide the mouse coordinates by the tile size.

Thank you, this helps, but now it throws my collision detection off.