2
\$\begingroup\$

I have a tilemap and am trying to proceduraly generate islands on it by changing the texture of the tiles and marking it as land ( I want to do this without texture atlas for now). How do you change the texture of an individual tile and how do you mark the tile to distinguish it from other tiles within map as land . I have tried to do this but for some reason even though I reference an individual tile all tiles are changing textures.

private static TiledMapTileLayer baseLayer;
private static TextureRegion landTextureReg;
private static Texture landTexture;
private static TiledMapTile landTile;

public static TiledMap generateMap(){
landTexture = new Texture(Gdx.files.internal("hex_land_trans.png"));
landTextureReg = new TextureRegion(landTexture);
TiledMap map = new TmxMapLoader().load("ocean_hex_map.tmx");
baseLayer = (TiledMapTileLayer)map.getLayers().get(0);
baseLayer.getCell(1, 1).getTile().setTextureRegion(landTextureReg);
return map;
}
\$\endgroup\$
  • \$\begingroup\$ How are you currently referencing your tiles? Can you provide some code on what you are currently doing? \$\endgroup\$ – jgallant Nov 17 '15 at 16:49
  • \$\begingroup\$ Added code to question :) \$\endgroup\$ – TheMagicShroom Nov 17 '15 at 17:06
  • \$\begingroup\$ @Jon can you see the code now? and sorry didnt know to put "@Jon" for you to get notifications :/ at least now i know :) \$\endgroup\$ – TheMagicShroom Nov 17 '15 at 18:25
  • \$\begingroup\$ Yes, so you are building your maps in Tiles, and then importing them. I don't know much about Tiled, but you should be able to set a Tile Type in there and then load the texture based on the type you set. \$\endgroup\$ – jgallant Nov 17 '15 at 18:37
  • \$\begingroup\$ @Jon I set the land tile as a tile type in tiled how do I mark certain tiles as land for later reference and change individual tile textures/types? \$\endgroup\$ – TheMagicShroom Nov 17 '15 at 18:40
0
\$\begingroup\$

Don't change the texture region of the TiledMapTile, change the TiledMapTile of theCell. Changing the tile itself will as you say cause allCells that reference thatTiledMapTile` to be updated.

private static TiledMapTileLayer baseLayer;
private static TextureRegion landTextureReg;
private static Texture landTexture;
private static TiledMapTile landTile;

public static TiledMap generateMap(){
    landTexture = new Texture(Gdx.files.internal("hex_land_trans.png"));
    landTextureReg = new TextureRegion(landTexture);
    TiledMap map = new TmxMapLoader().load("ocean_hex_map.tmx");
    TiledMapTileSet tileSet = map.getTileSets().getTileSet("your_tile_set");
    baseLayer = (TiledMapTileLayer)map.getLayers().get(0);


    Cell newCell = new Cell();
// Do this if the landTexture is in the tileset already but just not used for the
    // specific tile (you likely want all your tiles in one tilesheet).
    newCell.setTile(tileSet.getTile(42)); // Or whatever the ID you're trying to change to

// This would work if you want to load the tile sperately as you seem to do right now
StaticTiledMapTile myTile = new StaticTiledMapTile(landTextureReg);
    newCell.setTile(myTile); 

    // Set the cell at the coordinates you want to affect to a new tile, this won't change all the tile, just this cell
baseLayer.setCell(1, 1, newCell); 
    return map;

}

\$\endgroup\$
  • \$\begingroup\$ will try when I get home thankyou :) and how would I go about setting some sort of marker for which tiles are which terrain? \$\endgroup\$ – TheMagicShroom Nov 18 '15 at 10:22
  • \$\begingroup\$ If you're using the Tiled map editor, right click your tile in the tile set and select Properties. Then you can add custom properties (key-value pairs) that can be read from the tile by calling getProperties() on the tile. \$\endgroup\$ – bornander Nov 18 '15 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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