1

Can someone explain me how to detect a click on an object? I have already seen an answer to this question but it does not work.

public class TiledMapActor extends Actor {

    private TiledMap tiledMap;

    private TiledMapTileLayer tiledLayer;

    private TiledMapTileLayer.Cell cell;

    public TiledMapActor(TiledMap tiledMap, TiledMapTileLayer tiledLayer, TiledMapTileLayer.Cell cell) {
        this.tiledMap = tiledMap;
        this.tiledLayer = tiledLayer;
        this.cell = cell;
    }

}

public class TiledMapClickListener extends ClickListener {

    private TiledMapActor actor;

    public TiledMapClickListener(TiledMapActor actor) {
        this.actor = actor;
    }

    @Override
    public void clicked(InputEvent event, float x, float y) {
        System.out.println(actor.cell + " has been clicked.");
    }
}




public class TiledMapStage extends Stage {

    private TiledMap tiledMap;

    public TiledMapStage(TiledMap tiledMap) {
        this.tiledMap = tiledMap;

        for (MapLayer layer : tiledMap.getLayers()) {
            TiledMapTileLayer tiledLayer = (TiledMapTileLayer)layer; //THE ERROR IS IN THIS LINE
            createActorsForLayer(tiledLayer);
        }
    }

    private void createActorsForLayer(TiledMapTileLayer tiledLayer) {
        for (int x = 0; x < tiledLayer.getWidth(); x++) {
            for (int y = 0; y < tiledLayer.getHeight(); y++) {
                TiledMapTileLayer.Cell cell = tiledLayer.getCell(x, y);
                TiledMapActor actor = new TiledMapActor(tiledMap, tiledLayer, cell);
                actor.setBounds(x * tiledLayer.getTileWidth(), y * tiledLayer.getTileHeight(), tiledLayer.getTileWidth(),
                        tiledLayer.getTileHeight());
                addActor(actor);
                EventListener eventListener = new TiledMapClickListener(actor);
                actor.addListener(eventListener);
            }
        }
    }
}

Stage stage = new TiledMapStage(tiledMap);
Gdx.input.setInputProcessor(stage);

I have tried it with this code but I get this error message:

 com.badlogic.gdx.maps.MapLayer cannot be cast to com.badlogic.gdx.maps.tiled.TiledMapTileLayer

I do not understand how select which object is clickable

Sebastian
  • 5,721
  • 3
  • 43
  • 69

1 Answers1

1

As you already found out, the problem is your casting here:

for (MapLayer layer : tiledMap.getLayers()) {
    TiledMapTileLayer tiledLayer = (TiledMapTileLayer)layer;

Looks like the layers are not of type TiledMapTileLayer - or at least not all of them. (Afaik there are also ObjectLayers in a TiledMap) The simplest thing to get your code running again would ab an instanceof check:

for (MapLayer layer : tiledMap.getLayers()) {
    if (layer instanceof TiledMapTileLayer) {
        TiledMapTileLayer tiledLayer = (TiledMapTileLayer)layer;
        createActorsForLayer(tiledLayer);
    }
Sebastian
  • 5,721
  • 3
  • 43
  • 69
  • thanks now work but if i click on point with black screen there is a print of "has been clicked" , and if i want detect the touch with object layer? you can help me? – gianlucavallo Jun 05 '19 at 13:20
  • You should debug which types of layers you have in the map. Then you have to add special handling for these layer types. – Sebastian Jun 05 '19 at 14:00
  • if you have time, you want to see again the code because it isn't precise, if i click on a point of a screen that have not a tilelayer such as a black space in the screen i have "has been clicked" message – gianlucavallo Jun 05 '19 at 15:05
  • Just a guess: You should check if you correctly convert screen/mouse coordinates to tilemap coordinates. Depends on your camera viewport. – Sebastian Jun 06 '19 at 08:45
  • thanks you were right, in my viewport worlWidth and worldHeight is dived by 100, so in the actor.setBounds i must divided by 100, I'm sure that the position is right because i have draw an image with the cordinates and the dimension used with actor.setBounds and the Image overlaps the tile, but the click is not detected, you have some idea(SORRY MY BAD ENGLISH AGAIN) – gianlucavallo Jun 06 '19 at 20:02
  • and with the code that i tought you think that i work when i move my player in the map – gianlucavallo Jun 06 '19 at 22:59
  • 1
    I resolve, in TiledMapStage i had to add the viewport and super(viewport), thanks for your help – gianlucavallo Jun 07 '19 at 15:40