Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
6  
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... –  Byte56 Jul 17 '12 at 4:10
    
@corey take a look at the 2nd part of my answer may that might help? –  AbstractChaos Jul 18 '12 at 15:04
    
It still doesn't change the texture after adding that –  Corey Jul 19 '12 at 3:34

1 Answer 1

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;
}
share|improve this answer
1  
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. –  Richard Marskell - Drackir Jul 17 '12 at 11:43
    
@RichardMarskell-Drackir good spot :) –  AbstractChaos Jul 17 '12 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.