1

I am developing a Java game, and am currently writing a map maker. I can make the map and draw tiles, but i need to be able to change the position of those tiles so the character can see different locations of the map. When I try to change it, in the moveMap() method, It gives me this error:

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 570
at Base.moveMap(Base.java:88)
at Base.run(Base.java:55)
at java.lang.Thread.run(Unknown Source)

I have no idea why this is happening - could someone please help me understand the problem. Is there any alternate way to move the tiles?

Here is my code...

public class Base extends JPanel implements Runnable {

    private static String[] line = { 
        "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwwwwwwwwwwwfffffffffffwwwwwwwwwww", 
        "wwwwwwffwwwwwwwwfwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwfffffffwwwfwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwffwwwffffffwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwffwwwffffffwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwfffffffwwwwwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwffwwwwwwwwwwwwwwwwwwwwwwwwwwwwww", 
        "wwwwwwffwwwwwwwwwwwwwwffffffffwwwwwwww",
        "wwwwwwffwwwwwwwwwwwwwwwwwwwwffwwwwwwww",
        "wwwwffffffwwwwwwwwwwwwwwwwwwffwwwwwwww",
        "wwwwffffffffffffffffffffffffffwwwwwwww",
        "wwwwffffffwwwwwwwwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
        "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",};

    private Rectangle[] colRect;

    private int tileWidth = 30;
    private int tileHeight = 30;

    public Base() {
        colRect = new Rectangle[line.length * line[0].length()];
        for (int i = 0; i < line.length; i++) {
            for (int f = 0; f < line[i].length(); f++) {
                colRect[counter] = new Rectangle(f * tileWidth, i * tileHeight,tileWidth, tileHeight);
                if (counter != colRect.length) {
                    counter += 1;
                }
            }
        }
    }

    public void moveMap(){
        for(int i = 0; i <= colRect.length; i++){
            colRect[i].setLocation(colRect[i].x+1, colRect[i].y+1);
        }
    }
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Matthew Tory
  • 1,306
  • 2
  • 16
  • 30

2 Answers2

3

You almost certainly mean this:

for (int i = 0; i < colRect.length; i++) {

Instead of this:

for(int i = 0; i <= colRect.length; i++){

Remember that if an array has length n, the indexes go from 0 to n - 1.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • That worked! but do you know how I could change the position of the all of the tiles? so it moves around? – Matthew Tory Apr 07 '12 at 04:14
  • 1
    You'd have to explain what you're trying to do with moveMap() - are you trying to rotate it 90 degrees, move it left, move it up,... etc. Once we know what you're trying to do, we might be able to help you code it better. – wattostudios Apr 07 '12 at 06:14
0

I would suggest you just to modify the accessor to your geometry, than the map itself. Because it looks like, the function moveMap could have some kind of "loop" behaviour

getLocation (int index, offset = 0){
    int accIndex = (index + offset) % colRect.length;
    // ... probably better to modify your data-structure to simplify the handling
}
loybert
  • 416
  • 1
  • 3
  • 12