2

The answer here was exactly what I was looking for, a nice way to turn tiles into actors.

LibGDX: How to make tiled map tiles clickable?

I have a problem with this though, and that is that it only seems to work on one layer. From the code it seems like it should loop through and deal with all the layers, but that doesn't seem to be the case.

My problem right now is that if I add a second layer on top of my main layer in tiled, it will only "see" that layer. It works fine with one layer.

Edit: Both layers will be shown visually, but I can only interact with the top layer. The best thing would be if I could have my top layer be ignored when making actors, because it's mostly going to be used for transparent tiles and stuff like that.

Community
  • 1
  • 1
sistergodiva
  • 23
  • 1
  • 3

3 Answers3

1

Probably if you place two actors on top of each other, mouse clicks will only go to the top-most one? In that case, you'll only want to create one layer of actors (or even only one actor for your entire map) and then in its clicked override, figure out which tile / layer you want to deal with.

Thorbjørn Lindeijer
  • 2,022
  • 1
  • 17
  • 22
  • You are supposed to be able to let a click pass through, but I am not sure how to change the clickListener to do that, since it doesn't return anything. The other listeners return a true/false depending on if the click was handled and should stop, or go to the next layer. – sistergodiva Jan 22 '16 at 15:29
  • Maybe you can do this by implementing an `InputListener` instead of a `ClickListener`. You lose some convenience but you gain some control. I do not know whether it'll work though. Anyway, having only a single click handler and figuring out the tile coordinates from the click location should be quite a bit faster still. – Thorbjørn Lindeijer Jan 22 '16 at 20:45
1

Based on that example article (LibGDX: How to make tiled map tiles clickable?) you could do one of the following:

1) Force all of your top-layer cells to use an IgnoreTileListener (explained below)

2) Assuming you're using a static tile map or can otherwise add a field to your tile cell object, add a boolean field named 'ignored' and then when TiledMapStage.createActorsForLayer() is called, check if the cell is ignored and use that to determine if you wire its actor with an IgnoreTileListener or your existing TiledMapClickListener

The "IgnoreTileListener" can be either an InputListener or a ClickListener, your choice, but you'll be overriding touchDown() on the IgnoreTileListener so you can force that to return false, thus leaving the event unhandled so your desired actor can listen for the event.

Either way, you're effectively using InputListener since ClickListener inherits from InputListener anyway.

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/ClickListener.html

Whatever you end up using to get your desired output, I'd be curious to hear (especially if you end up using this approach!)

Community
  • 1
  • 1
Sam Abazly
  • 76
  • 4
0

Have you debugged you application? Does your code fail generating actors for every layer or have you just problems to click the bottom layer? I'm not sure if the code you linked really checks if there is a cell at the current position before it creates an actor.

Sebastian
  • 5,721
  • 3
  • 43
  • 69
  • System.out.println(actor.cell + " has been clicked." + actor.cell.getTile().getId()); gives null if I click a tiles on the bottom layer. I checked the debug and it seems to at least create twice as many actors if I add a second layer, so it seems it's at least doing that. – sistergodiva Jan 22 '16 at 15:18