LibGDX:如何使平铺地图图块可点击? [英] LibGDX: How to make tiled map tiles clickable?
问题描述
如何为平铺地图中的切片添加点击侦听器,以便在使用鼠标选择切片时它会突出显示?
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?
推荐答案
libGDX不直接支持,因为TiledMap的东西只用于渲染。
That's not supported directly by libGDX as the TiledMap stuff is only for rendering.
你可以轻松创建一个 Stage $ c但是,$ c>,它将作为TiledMap的某种覆盖输入层。只需为每个图块创建一个
Actor
,该图块与该图块的位置大小相同。然后,您可以将 EventListener
添加到这些演员,以便能够识别这些演员的点击等内容。
You could easily create a Stage
though, which will act as some kind of overlay-input-layer for your TiledMap. Just create an Actor
for each tile which has the same size as position as that tile. Then you are able to add EventListener
s to those actors to be able to recognize things like clicks on those actors.
那些演员应该保留对其起源的引用,即 TiledMapTileLayer.Cell
。因此,您可以在处理这些事件时随时从演员返回到单元格。
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:
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;
}
}
这个小听众可以附加到其中一个演员并将做任何逻辑:
This little listener can be attached to one of those actors and will do any kind of logic:
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.");
}
}
以下课程实际上是从给定地图创建演员并将它们连接到监听器:
The following class actually creates the actors from a given map and wires them to the listeners:
public class TiledMapStage extends Stage {
private TiledMap tiledMap;
public TiledMapStage(TiledMap tiledMap) {
this.tiledMap = tiledMap;
for (MapLayer layer : tiledMap.getLayers()) {
TiledMapTileLayer tiledLayer = (TiledMapTileLayer)layer;
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);
}
}
}
}
现在 TiledMapStage
将为您完成所有工作。您需要做的就是:
Now the TiledMapStage
will do all work for you. All you need to do is the following:
Stage stage = new TiledMapStage(tiledMap);
Gdx.input.setInputProcessor(stage);
在渲染(...)中你需要调用 stage.act ()
。请记住,当您用于渲染TiledMap时,为舞台使用相同的视口
。否则输入和渲染的地图将不会对齐。
And in render(...) you need to call stage.act()
. Remember to use the same Viewport
for the stage as you are using to render the TiledMap. Otherwise the input and your rendered map won't be aligned.
这篇关于LibGDX:如何使平铺地图图块可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!