3
\$\begingroup\$

I'm making a 2D game with libGDX, and I'm using its TileMapRenderer to render my map which I have made in the map editor Tiled. In Tiled, I have a dedicated collision layer. However, I can't figure out how I'm supposed to hide it and its tiles in the game.

This is how a map is loaded:

TiledMap map = TiledLoader.createMap(Gdx.files.internal("maps/map.tmx"));
TileAtlas atlas = new TileAtlas(map, Gdx.files.internal("maps"));
tileMapRenderer = new TileMapRenderer(map, atlas, 32, 32);

Currently the collision tiles are rendered on top of everything else, as I see them in the map editor.

\$\endgroup\$
6
\$\begingroup\$

At this time, you'd have to create an int array of the layer indexes you do want to draw, and then use the render method which takes a camera and the layer indexes.

So, if you have three layers and the last one is the collision layer you do not wish to draw, you'd do:

int[] layers = new int[] {0, 1}; // preferably outside of the render loop, to avoid gc

then when you wish to render:

tileMapRenderer.render(camera, layers);

This is certainly less than ideal as the layer itself should have a visibility flag (as it does in tiled) which would be used to determine if the layer should be drawn as opposed to relying on whether or not the index is passed in to the render function.

I've been working on a new version of the Tiled module in LibGDX which hopefully will make it easier to work with.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Please add the TSX tile set support to ones :) And support to load unpacked maps for debugging/creating/testing purposes :) \$\endgroup\$ – asktomsk Nov 20 '12 at 13:15
  • 2
    \$\begingroup\$ @asktomsk : My working copy loads TSX files, so that'll definitely be in there. If you use SimpleTileAtlas you don't have to use the packer. Your mileage may vary, but I've not heard of there being any problems with that approach. \$\endgroup\$ – nEx.Software Nov 20 '12 at 13:30
1
\$\begingroup\$

Easiest way is to set the visibility to false tiledMap.getLayers().get(index).setVisible(visible);

EDIT: Reference to the libGDX API http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/MapLayer.html#setVisible-boolean-

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Can you link to the documentation? Your answer suggests that the accepted answer is out-of-date, so a reference would be good. \$\endgroup\$ – Seth Battin Sep 14 '14 at 16:05
0
\$\begingroup\$

In Lua, I set the opacity to zero or the named layer, like:

if map.layers["collision"] then
   map.layers["collision"].opacity = 0
end

You need to search in libgdx how to get a layer by name, and then set its opacity.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can also try hiding your collision layer by using this

TiledLayer collision;
// [...]

// search the collision layer
for(int i=0;i<map .layers.size();i++){
    if(map.layers.get(i).name.equals("collision")){
        collision=map.layers.get(i);
        map.layers.remove(i); // remove the collision layer.
        break;
    }
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

The easiest way to do this it to simply not hide them at all. You should create a set of blank tiles on your PNG tile sheet. Use the blank tiles to create your collision layer and render it. Since all the tiles you use are transparent in nature, the image it writes is nothing.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

If, when in the Tiled IDE, you click the eye in the layers tab.

It will deselect the layer as being visible both inside the IDE and when used in a game engine.

All the layer data is still there and can be used for collision.

| improve this answer | |
\$\endgroup\$

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.