OK so my code is here: http://www.so.pastebin.com/m7V8rQ2n
What I want to know… let’s say I have an image which I can redraw on tiles… is there a way to check for future tiles so I DON’T go out of bounds of my already defined tile map?
Like if I were at the edge of a map… it would NOT let me go past it?
Thanks.
Generally speaking, preventing
ArrayIndexOutOfBoundsExceptioncan be done by simply making sure that the index is within the bound.Thus, a simple check like this is quite typical:
Barring nasty things like
arrgetting reassigned between the check and the access, the above code will NEVER throwArrayIndexOutOfBoundsException.2D array "boards"
Often, you can be a lot more specific, e.g. when you have rectangular "boards" stored in a two-dimensional array (or rather, array of arrays in Java).
Then you can have a method like the following:
The bound check is both easier to read and to write, since we know that we have an MxN board. If
isInBound(r, c), thenboard[r][c]will NEVER throwArrayIndexOutOfBoundsException.