简体   繁体   English

单击图块并在libGdx中更改其颜色

[英]Clicking on a tile and changing its color in libGdx

I'm trying to click on a tile and change its color. 我正在尝试单击瓷砖并更改其颜色。 So far I have this: 到目前为止,我有这个:

if(Gdx.input.isTouched()){
            Vector3 click = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(click);
            Cell clicked = path.getCell((int)click.x, (int)click.y);
            clicked.getTile();
    }

Once clicked has its tile coordinates (assuming I did this correctly) I now want to draw a blue square on that tile. clicked具有其图块坐标(假设我正确执行此操作),现在我想在该图块上绘制一个蓝色正方形。 How would I do this? 我该怎么做? I have testTile.png in my assets folder if that helps. 如果有帮助,我的资产文件夹中有testTile.png I have drawn other pictures to the screen using batch.draw(); 我已经使用batch.draw();将其他图片绘制到屏幕上了batch.draw(); . If anymore information is needed please feel free to ask. 如果需要更多信息,请随时询问。

path is the TiledMapTileLayer. path是TiledMapTileLayer。

The way I will approach this problem is having a list of custom class storing the locations where the blue square needs to be drawn and the sprite which needs to be drawn. 我要解决此问题的方法是有一个自定义类的列表,其中存储了需要绘制蓝色正方形和需要绘制精灵的位置。 (Storing sprite in list will give you more flexibility) So basically in your render method you will draw these tiles in by looping the List. (将精灵存储在列表中将为您提供更大的灵活性)因此,基本上,在您的渲染方法中,您将通过循环列表来绘制这些图块。

Example code : (Could not think of a better name than SpriteMap) 示例代码:(想不出比SpriteMap更好的名称)

public class SpriteMap {
   int posX;
   int posY;
   Sprite sprite;

   public SpriteMap(int posX, int posY, Sprite sprite) {
       this.posX = posX;
       this.posY = posY;
       this.sprite = sprite;
   }
   //ignoring getters
}

Let's say you have a list of SpriteMap in your class where you are going to render and also have access to the list in isTouched() method. 假设您在要渲染的类中有一个SpriteMap列表,并且还可以通过isTouched()方法访问该列表。

List<SpriteMap> spriteMapList = new ArrayList<SpriteMap>();

Now inside your isTouched 现在在您的isTouched内部

if(Gdx.input.isTouched()){
      Vector3 click = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(click);
      spriteMapList.add(new SpriteMap((int)click.x, (int)click.y, new Sprite(THE_BLUE_BOX_TEXTURE));
}

Now in your render method all you have to do is loop the list and draw each and every sprite from list from SpriteMap objects. 现在,在渲染方法中,您所需要做的就是循环列表,并从SpriteMap对象绘制列表中的每个精灵。

//Pseudo code inside render after batch.begin(); 
for(SpriteMap spriteMap : spriteMapList) {
    spriteMap.sprite().setPosition(spriteMap.posX, spriteMap.posY);
    spriteMap.draw(batch);
}

NOTE Re-use the blue box texture. 注意重新使用蓝框纹理。 Also make sure you draw blue box after you have drawn the map else it will get hidden behind the map. 另外,请确保在绘制地图后绘制蓝色框,否则它将被隐藏在地图后面。

ANOTHER NOTE The positions x and y from clicked may need to be adjusted for the sprite to be drawn on the exact tile. 另一个注意事项可能需要调整单击位置x和y,以便将精灵绘制在确切的图块上。 So do change that accordingly. 因此,请相应地进行更改。 I have directly used X and Y from click but you may have to change them. 我直接从点击中使用了X和Y,但是您可能不得不更改它们。

Hope that helps. 希望能有所帮助。

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

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