Tile engine Texture not updating when numbers in array change

Posted by Corey on Game Development See other posts from Game Development or by Corey
Published on 2012-07-16T23:41:56Z Indexed on 2012/09/16 3:54 UTC
Read the original article Hit count: 436

Filed under:
|
|

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 never ask a question until I have tried to figure it out myself. I have been stuck with this problem for two weeks. It's not like this site is made for asking questions or anything. So if you actually try to help me instead of telling me to use a debugger thank you. You either get told you have too much or too little code. Nothing is never enough for the people on here it's as bad as something like reddit. Idk 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. That is why I need help.

© Game Development or respective owner

Related posts about java

Related posts about textures