如何将瓷砖悬停在瓷砖上后变得更亮?

时间:2016-12-10 20:32:03

标签: libgdx tiled

我使用libGDX中的HexagonalTiledMapRenderer和Tiled程序创建了一个六边形等距平铺地图。地图正在正确呈现,但我不知道如何访问有关单个图块的信息,因此我不知道如何处理用户输入。

我想要的是将瓷砖悬停在它上面(也可以打印瓷砖的某些东西,就像它是什么瓷砖,即森林,河流,山脉),所以我想我需要一些类似网格的系统,我认为这将通过平铺地图给我,但我找不到它/了解它。

一些代码
主要核心课程

public class MyGdxGame extends Game {

 @Override
 public void create () {
    setScreen(new Play());
 }
}

上课

public class Play implements Screen {

 private TiledMap map, hexMap;
 private HexagonalTiledMapRenderer hexRenderer;
 private OrthographicCamera camera;

 @Override
 public void show() {
    hexMap = new TmxMapLoader().load("hexTiledMap.tmx");
    System.out.println(hexMap.getProperties().getKeys());
    hexRenderer = new HexagonalTiledMapRenderer(hexMap);

    camera = new OrthographicCamera();
    camera.setToOrtho(false);
 }

 @Override
 public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    hexRenderer.setView(camera);
    hexRenderer.render();
 }

 @Override
 public void resize(int width, int height) {
    camera.viewportWidth = width;
    camera.viewportHeight = height;
    camera.update();
 }
}

The hexagonal isometric tiled map

我想用它做的例子

例如,我希望能够使一块瓷砖更亮或更红,或者让它消失。所以我基本上想让瓷砖互动。我希望程序知道,例如,光标下的哪个图块。这些都只是一些例子,我希望你能理解我想要的东西。

1 个答案:

答案 0 :(得分:2)

这实际上是一个3部分问题。

选择图块

您有鼠标位置,相机位置和瓷砖大小。 如果根据摄像机位置平移鼠标位置,则可以获得地图的鼠标坐标。 然后取这些坐标并与tileize一起使用。将x和y值转换为Integer,并且您拥有鼠标悬停在其上的图块。

让照片亮起

一种简单的方法是使用半透明的瓷砖/精灵,并将其显示在您用鼠标悬停的瓷砖上方。

获取磁贴的属性:

TiledMap map;
TiledMapTileLayer tileLayer;

//define the layer where you select the tile
tileLayer = (TiledMapTileLayer) map.getLayers().get("layername");

//get the tile that you want
Cell cell = tileLayer.getCell(x,y);
TiledMapTile tile = cell.getTile();

//this is where you get the properties of the tile
tile.getProperties().get("propertiename");

在Tiled中定义属性的方法是在tileset中选择一个tile(或多个tile),右键单击并选择“Tile属性”。在属性窗口的左下角,您会看到一个加号。单击此按钮可以添加自定义属性(为其指定名称和类型)。 tileset中设置的属性将继承到tilemap中放置的每个tile。

例如:如果要定义图块的类型(树,沙,水等),请选择图块并添加名称为“TileType”的属性作为字符串。按ok键可以为类型指定值。前“沙”。

然后,当您想要所选图块的类型时,您会读取该属性:

String tileType = tile.getProperties().get("TileType");

这样,您可以在磁贴上设置许多属性。

如果您尝试从没有平铺的x,y位置从平铺贴图中获取单元格,则tileLayer.getCell(x,y)将返回null。所以记得检查一下。

相关问题