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. Would I have to write to the text file to notice the changes I made? Right now it changes the number in the array but the tile texture doesn't change. Do I have to do more than just change the number in the array?

public class Tiles {

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

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

private Image grass, dirt, fence, mound;

private SpriteSheet tileSheet;

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

Player player = new Player();

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(",");
    for (String str : values) {
            int str_int = Integer.parseInt(str);
            map[x][y]=str_int;
            //System.out.print(map[x][y] + " ");
            y=y+1;
        }
//System.out.println("");
x=x+1;
    y = 0;
}
    in.close();
}

public void update(GameContainer gc) {

}

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

Mouse picking

public void checkDistance(GameContainer gc) {
    Input input = gc.getInput();

    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;
        }
    }  

    System.out.println(tiles.map[(int)mousetileX][(int)mousetileY]);
}
share|improve this question

1 Answer 1

What seems a bit strange to me is the loop where you read the file. As I understood your input file looks something like

1, 2, 3
4, 4, 4
4, 4, 4

If I read your code right you assign the X axis from the file to the y axis inyour data structure. The bit above would be translated into

1, 4, 4
2, 4, 4
3, 4, 4

Try to switch the x and y value in the loop (use y for the outer and x for the inner loop).

Otherwise, the code looks like it should render as you expect it (besides this little thing). So maybe something is wrong with the texture loading... Have you tried to rendere every terrain texture? Or maybe try to render the level with a hardcoded value. If it doesn't work like that the problem will be in the area of image loading.

Also, use x++ and y++ instead of x = x + 1 ;) Not really an issue but it is more compact and (personally) i find it easier to read.

Edit: In the way you load your map currently, the first array dimension is used for the y axis and the second dimension is used for the x axis.

int[][] tiles = new int[HEIGHT][WIDTH];

I would switch the dimensions so likte this:

int[][] tiles = new int[WIDTH][HEIGHT];

When I read coordinates I read the x position first and I like it, when the code is also layed out like that. If you want to change it you would have to make two things:

1: In the init method, you have to increment (add 1) the x value when a character is read. Increment the y value after the complete line has been read.

while ((line = in.readLine()) != 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++;
        }
    y++;
}

2: Change the render method. The y loop needs to be outside the x loop. (I also added a bit so you can use different width and height values, but basically its best to store the value somewhere):

    int width = map.length;
    int height = map[0].length;
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x ++) {
            int textureIndex = map[y][x];
            Image texture = tiles[textureIndex];
            texture.draw(x*tileWidth,y*tileHeight);
        }
    }
share|improve this answer
1  
Ah, just saw something. You load the level inverted, but in the render loop you switched y and x positions in the array. You should be careful with that things. As soon as width and height of the level differ, you could run into problems. Try to apply the change in the loading i suggested above and change the rendering code from tile[y][x] to tile[x][y]... –  tom van green Jul 10 '12 at 12:42
    
So just do this map[y][x]=str_int;? Or do you mean in the for loop in the render method? Because if I do that is draws my world sideways like your second set of numbers there, but map[y][x]=str_int; renders correctly. I am already rendering every texture and they all work. I tried changing the rendering to tiles[map[x][y]].draw(xtileWidth,ytileHeight); to get rid of textureIndex but it still doesn't work. I'm not sure why the texture doesn't change –  Corey Jul 10 '12 at 17:18
1  
I added some clarification in the answer. –  tom van green Jul 10 '12 at 23:40
    
Thanks that will stop any confusion when using the array later on. Do you not have any ideas on why it is not changing the texture? Is there a way I could debug it to see why? –  Corey Jul 11 '12 at 0:00
    
I'm not entirely sure, but try to adapt my proposed changes to the code. In render you use map[y][x], in load you use map[x][y] etc. Get the code straight and edit your post (include the changed code). If it still won't change the tiles, maybe something is wrong with the tilesets. Try to render only the textures and see if they are loaded correctly. –  tom van green Jul 11 '12 at 11:44

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.