How can I add click listeners for the tiles from tiled map so that when you select a tile with the mouse it becomes highlighted?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
That's not supported directly by libGDX as the TiledMap stuff is only for rendering.
You could easily create a
Stage
though, which will act as some kind of overlay-input-layer for your TiledMap. Just create anActor
for each tile which has the same size as position as that tile. Then you are able to addEventListener
s to those actors to be able to recognize things like clicks on those actors.Those actors should keep a reference to their "origin", namely
TiledMapTileLayer.Cell
. So you are able to go back from the actor to the cell anytime when processing those events.The following shows how you might do it:
This Actor is responsible to catch the events and keep the information about the tile it's based on:
This little listener can be attached to one of those actors and will do any kind of logic:
The following class actually creates the actors from a given map and wires them to the listeners:
Now the
TiledMapStage
will do all work for you. All you need to do is the following:And in render(...) you need to call
stage.act()
. Remember to use the sameViewport
for the stage as you are using to render the TiledMap. Otherwise the input and your rendered map won't be aligned.