-1

So I'm just creating a Tower-Defence Game.

Now I'm trying to configure, that the player can click on a tile, and by doing that, an object gets attatched to this tile, and the texture of this object is used for the tile.

And I don't know how to do that.

I don't know how to take onclick events, since you cant set those on tiles, maps, cells etc. ... (I tried to create an Actor in the size of the tiledmap, but I don't know how to convert the x and y position to the correct tile...)

I thought about working with MapObjects, but I just don't understand those and I couldn't find a good explenation online.

FeRo2991
  • 41
  • 4

1 Answers1

0

You should check for mouse clicks and handle it as needed:

To see if the mouse is pressed, use:

boolean mouseLeftDown = Gdx.input.isButtonPressed(Input.Buttons.LEFT);

To get the position of the mouse, use:

int mouseX = Gdx.input.getX();
int mouseY = Gdx.input.getY();

Now that you have the position of the mouse, you can figure out the tile under the mouse. For example, if you have Tile objects in a 2D array with each tile being 64 by 64, you can do this to the tile:

Tile clickedTile = tiles[mouseX/64][mouseY/64];
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75