0

I have been trying to add functionality to my libGdx game to get the properties of a tile when I click on a cell. I used this tutorial on how to add mouse interaction to my tilemap. My problem is the tile's properties can be accessed within the first nested for loop, but when I try to access the properties again it returns null. I tried accessing the cell manually, which also failed :(

    AnimatedTiledMapTile animCloudTMap = new AnimatedTiledMapTile(1 / 12f,  fcloudTiles);
    AnimatedTiledMapTile animSoldierTMap = new AnimatedTiledMapTile(1 / 16f,  fSoldierTiles);
    AnimatedTiledMapTile animCmdTMap = new AnimatedTiledMapTile(1 / 12f,  fCmdiles);
    AnimatedTiledMapTile animWallTMap = new AnimatedTiledMapTile(1 / 12f,  fWallTiles);

    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    //System.out.println(layer.getName());

    for(int x = 0; x < layer.getWidth(); x++) {
        for(int y = 0; y < layer.getHeight(); y++) {
            Cell cell = layer.getCell(x, y);
            if(cell != null) {
                //System.out.println("First "+x +", "+ y);
                TiledMapTile tile = cell.getTile();
                if(tile.getProperties().containsKey("animated") 
                        && tile.getProperties().get("animated",String.class).equals("true")) {
                    if(tile.getProperties().get("name",String.class).equals("cloud")) {
                        cell.setTile(animCloudTMap);
                    }
                    if(tile.getProperties().get("name",String.class).equals("cmd")) {
                        cell.setTile(animCmdTMap);
                    }
                    if(tile.getProperties().get("name",String.class).equals("soldier")) {
                        cell.setTile(animSoldierTMap);
                    }
                    if(tile.getProperties().get("name",String.class).equals("wall")) {
                        cell.setTile(animWallTMap);
                    }

                }
                tile.getProperties().put("X", x);
                tile.getProperties().put("Y", y);
                System.out.println(tile.getProperties().get("name", String.class));
            }
        }
    }

    for(int x = 0; x < layer.getWidth(); x++) {
        for(int y = 0; y < layer.getHeight(); y++) {
            Cell cell = layer.getCell(x, y);
            if(cell != null) {
                TiledMapTile tile = cell.getTile();
                System.out.println(tile.getProperties().get("name", String.class));
            }
        }
    }

    rend = new OrthogonalTiledMapRenderer(map, .125f);
    rend.setView(cam);

    //Impose an input grid
    stage = new TiledMapStage(map);
    stage.getViewport().setCamera(cam);
    Gdx.input.setInputProcessor(stage);

Here is the output:

cloud
 ...
cloud
null
 ...
null

1 Answers1

1

You are rewriting cell tile

1) here cell.setTile(animCloudTMap);

2) here cell.setTile(animCmdTMap);

3) here cell.setTile(animSoldierTMap);

4) and here cell.setTile(animWallTMap);

with new tile with NO properties.

Your first loop worked because you get it TiledMapTile tile = cell.getTile(); before rewriting.

icarumbas
  • 1,777
  • 3
  • 17
  • 30
  • Ah I see, thank you for the quick response! I simply added this code to the if statements: MapProperties mp = tile.getProperties(); cell.setTile(animWallTMap); tile = cell.getTile(); tile.getProperties().putAll(mp); Then it worked! – Matthew Clark Oct 28 '17 at 22:16