Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX: How to do animations on an isometric tiled map?

I've some problem with LibGDX and tiled maps. I come from Corona SDK (Lua) and I've used Lime (third party lib to handle tmx maps), now I switched to LibGDX and followed this really recent feature: GraphicsTileMaps

I was able to load an isometric map from assets using IsometricTiledMapRenderer as renderer and an OrthographicCamera to display the map (all works as described in the wiki page). I was also able to fill programmatically some tile on the map layer, but my problem starts, when I try to use AnimatedTiledMapTile:

    map = new TmxMapLoader().load(...);

    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().getLayer("layer1");
    TiledMapTileSet tileset =  map.getTileSets().getTileSet("tileset1");
    TiledMapTile grassTile =  tileset.getTile(4);
    TiledMapTile rockTile =  tileset.getTile(6);

    Array<StaticTiledMapTile> playerTileArr = new Array<StaticTiledMapTile>();
    playerTileArr.add((StaticTiledMapTile)tileset.getTile(7));
    playerTileArr.add((StaticTiledMapTile)tileset.getTile(8));

    Cell grass = new Cell();
    grass.setTile(grassTile);

    Cell player = new Cell();
    player.setTile(new AnimatedTiledMapTile(0.1f, playerTileArr));

    layer.setCell(0,0, grass);  // <--- This works.
    layer.setCell(0,1, player); // <--- When I try this, it causes 
                                //      a NullPointerException.

    renderer = IsometricTiledMapRenderer(map, 1 / 30f);
    ...

Can someone help? I found an AnimatedTiledMapTile usage example for TideMapLoader but I know I'm using a "tiled map" (not a "tide map"). However.. someone can explain how I can make an animation on a specific tile?

like image 390
Felice Ostuni Avatar asked Mar 10 '13 14:03

Felice Ostuni


1 Answers

Been by this a few time searching my self. But got it to work now.

Best guess is that your tileset don't have 7 or 8 tiles.

I got the following code to work:

    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    TiledMapTileSet tileset = map.getTileSets().getTileSet(0);

    Array<StaticTiledMapTile> at = new Array<StaticTiledMapTile>();
    if (tileset.getTile(0) != null) at.add((StaticTiledMapTile) tileset.getTile(0));
    if (tileset.getTile(1) != null) at.add((StaticTiledMapTile) tileset.getTile(1));
    if (tileset.getTile(2) != null) at.add((StaticTiledMapTile) tileset.getTile(2));
    if (tileset.getTile(3) != null) at.add((StaticTiledMapTile) tileset.getTile(3));

    layer.getCell(2, 2).setTile(new AnimatedTiledMapTile(0.3f, at));

Only getTile(1) and getTile(2) was != null - I only had 2 tiles. (my trouble was that getTile(0) wasnt the first tile.. getTile(1) was!)

like image 84
Steffen Avatar answered Nov 17 '22 11:11

Steffen