简体   繁体   English

如何在LibGDX的图块贴图中突出显示某些单元格?

[英]How can I highlight some cells in a Tile Map in LibGDX?

I'm programming a Strategy RPG using LibGDX, and Tiled as a map editor. 我正在使用LibGDX编程Strategy RPG,并使用Tiled作为地图编辑器。 I am loading the Tiled map with the Asset Manager and right now I don't have a Texture Atlas (I'm not quite sure yes which will be my definitive textures). 我正在用Asset Manager加载Tiled贴图,现在我没有Texture Atlas(我不太确定是的,这将是我确定的贴图)。

If you are familiar with Strategy RPG games, you should know that when you want to move a character, the cells where you can move it change color. 如果您熟悉Strategy RPG游戏,则应该知道,要移动角色时,可以移动角色的单元会更改颜色。 That's what I'm trying to do. 那就是我想做的。

But before figuring that, I'm trying to click a cell and change it's color by changing the texture of the upper layer of that cell, with the following code: 但是在弄清楚这一点之前,我想通过单击以下单元格来单击一个单元格并通过更改该单元格上层的纹理来更改其颜色:

TiledMapTileLayer.Cell selectedCell = new TiledMapTileLayer.Cell();
selectedCell.setTile(selectedTileSet.getTile(0));
StaticTiledMapTile selectedTile = new StaticTiledMapTile(selectedSpriteRegion);
selectedCell.setTile(selectedTile);
selectedTileLayer.setCell((int) Math.floor(screenX / 32), (int) Math.floor((viewport.getScreenHeight() - screenY / 32)), selectedCell);

That code is inside a TouchDown inside a Screen , to be called every time I touch a part of the Screen . 该代码位于Screen内的TouchDown内,每次我触摸Screen的一部分时都会调用该代码。

The layer I use is one I set specifically for the highlighted tiles and it is above the rest of the layers in my code, except for the one that has the characters. 我使用的图层是我专门为突出显示的图块设置的图层,它在代码中其余各层的上方,除了具有字符的图层。

Thanks in advance! 提前致谢!

I found the error. 我发现了错误。

Here is the new code: 这是新代码:

TiledMapTileLayer.Cell selectedCell = new TiledMapTileLayer.Cell();
selectedCell.setTile(selectedTileSet.getTile(1765));
StaticTiledMapTile selectedTile = new StaticTiledMapTile(selectedSpriteRegion);
selectedCell.setTile(selectedTile);
selectedTileLayer.setCell(screenX / 32, (viewport.getScreenHeight() - screenY) / 32, selectedCell);

So there were two errors. 因此,存在两个错误。 First that the id of the tile was wrong. 首先, id贴的id错误。 I thought that because I had one tile, it would be zero, but no, it is 1765 (don't ask me why). 我以为,因为我只有一个图块,所以它为零,但不,它是1765年(不要问我为什么)。

Also, I was setting the wrong cell since the formula I used was wrong. 另外,由于我使用的公式错误,因此我设置的单元格错误。 Math.floor wasn't necessary and I was dividing before subtracting in the Y position (can't believe I did that). Math.floor不是必需的,我在除以Y位置之前就进行了除法(不敢相信我这样做了)。

That's all! 就这样! Hope this helps to someone with a similar error in the future! 希望这对将来有类似错误的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM