-2
\$\begingroup\$

I draw my map from a txt file. I am using java with slick2d library. When I print the array the number changes in the array, but the texture doesn't change.

public class Tiles {

public Image[] tiles = new Image[5];

public int[][] map = new int[64][64];

public Image grass, dirt, fence, mound;

private SpriteSheet tileSheet;

public int tileWidth = 32;
public int tileHeight = 32;

public void init() throws IOException, SlickException {
    tileSheet = new SpriteSheet("assets/tiles.png", tileWidth, tileHeight);

    grass = tileSheet.getSprite(0, 0);
    dirt = tileSheet.getSprite(7, 7);
    fence = tileSheet.getSprite(2, 0);
    mound = tileSheet.getSprite(2, 6);
    tiles[0] = grass;
    tiles[1] = dirt; 
    tiles[2] = fence;
    tiles[3] = mound;

    int x=0, y=0;
BufferedReader in = new BufferedReader(new FileReader("assets/map.dat"));
String line;
while ((line = in.readLine()) != null) {
        String[] values = line.split(",");
        x = 0;
    for (String str : values) {
            int str_int = Integer.parseInt(str);
            map[x][y]=str_int;
            //System.out.print(map[x][y] + " ");
            x++;
        }
//System.out.println("");
y++;
}
    in.close();
}

public void update(GameContainer gc) {

}

public void render(GameContainer gc) {
    for(int y = 0; y < map.length; y++) {
        for(int x = 0; x < map[0].length; x ++) {
            int textureIndex = map[x][y];
            Image texture = tiles[textureIndex];
            texture.draw(x*tileWidth,y*tileHeight);
        }
    }
}

}

Mouse Picking Where I change the number in the array

Input input = gc.getInput();

    gc.getInput().setOffset(cameraX-400, cameraY-300);
    float mouseX = input.getMouseX();
    float mouseY = input.getMouseY();

    double mousetileX = Math.floor((double)mouseX/tiles.tileWidth);
    double mousetileY = Math.floor((double)mouseY/tiles.tileHeight);

    double playertileX = Math.floor(playerX/tiles.tileWidth);
    double playertileY = Math.floor(playerY/tiles.tileHeight);

    double lengthX = Math.abs((float)playertileX - mousetileX); 
    double lengthY = Math.abs((float)playertileY - mousetileY);
    double distance = Math.sqrt((lengthX*lengthX)+(lengthY*lengthY)); 

    if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && distance < 4) {
        System.out.println("Clicked");  
        if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
            tiles.map[(int)mousetileX][(int)mousetileY] = 0;
        }
    } 

I don't know what is wrong: all my textures work when I render them, it just doesn't update when the number in the array changes. I am obviously debugging when I say that I was printing the array and the number is changing like it should, so it's not a problem with my mouse picking code. It is a problem with my textures, but I don't know what because they all render correctly.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ Code dump find my problem are not good questions. Step through it in the debugger. see what the values are going in and what the values are coming out. Make sure you're clicking on dirt too... \$\endgroup\$
    – House
    Commented Jul 17, 2012 at 4:10
  • \$\begingroup\$ @corey take a look at the 2nd part of my answer may that might help? \$\endgroup\$ Commented Jul 18, 2012 at 15:04
  • \$\begingroup\$ It still doesn't change the texture after adding that \$\endgroup\$
    – Corey
    Commented Jul 19, 2012 at 3:34

1 Answer 1

0
\$\begingroup\$

The problem could be in your render logic you have hardcoded to look at map[0]

Change to

    for(int y = 0; y < map.length; y++) {
        for(int x = 0; x < map[y].length; x ++) {
            int textureIndex = map[x][y];
            Image texture = tiles[textureIndex];
            texture.draw(x*tileWidth,y*tileHeight);
        }
    }

EDIT I as the comment below suggests map[0].length would be fine if the map is guaranteed to be same width each row. (this is what I for posting before morning coffee)

EDIT 2

It dawned on me that you have no option to update back to 1 if 0 in your click handler so you could never change a 0 to a 1 in the first place, so the number never changed

if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
  tiles.map[(int)mousetileX][(int)mousetileY] = 0;
}

you could add an else statement like so

if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
  tiles.map[(int)mousetileX][(int)mousetileY] = 0;
}
else {
   tiles.map[(int)mousetileX][(int)mousetileY] = 1;
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think it's assumed that the map doesn't have a staggered width and so the length of "row" 0 should be the same length as any other row. \$\endgroup\$ Commented Jul 17, 2012 at 11:43
  • \$\begingroup\$ @RichardMarskell-Drackir good spot :) \$\endgroup\$ Commented Jul 17, 2012 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .