How would I make one of the tiles disappear from this array?

public TileGrid() {
		map = new Tile[20][15];
		for(int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				map[i][j] = new Tile (i * 64, j * 64, 64, 64, TileType.futurefloor);
			}
		}
	}
	
	public void Draw() {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				Tile t = map[i][j];
				DrawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());
			}
		}
	}

I’ve moved from Java software rendering to LWJGL which someone suggested on one of my newb posts, I’m finding it more fun and effective. I’ve got a question here about a tilemap array I’ve managed to draw to the screen.

How would I go about removing tiles, lets say upon a mouse click?

Thanks

Step 1: Use a MouseListener (I’m not familiar with LWJGL, so it might have an internal MouseListener you can hook into) to detect when the user clicks.

Step 2: Use that click event to figure out where on the screen the user clicked. Translate that point to a cell in your array.

Step 3: Disable/remove that cell.

If you’re having problems with step 2, then get out some grid paper and draw an example grid. Make some random points in that grid, and translate them to their position in the array. Repeat that until you’ve found a pattern.

Check for mouse click events, check if the X and Y click is within the bounds of the tile, set a flag in the tile class so that it can not be drawn.

Something like:


if(mouse.x > tile.x && mouse.x < tile.x + tile.width && mouse.y > tile.y && mouse.y < tile.y + tile.height){
    tile.setVisible(false);
}

You could even set a flag to deactivate the tile, so that it can not receive any more clicks/updates:


if((tile.isActive()) && mouse.x > tile.x && mouse.x < tile.x + tile.width && mouse.y > tile.y && mouse.y < tile.y + tile.height){
    tile.setVisible(false);
    tile.setActive(false);
}

Something like this should do the trick, pseudo tho so probably some typos in there.

Thank you

I have 2 questions regarding this, would it be easier, for the future… to use arraylist for tilemaps? that way you can delete them?

or…

Changing the value of the array, lets say:

0 0 0 0 0
1 1 1 0 1
1 1 1 0 1
0 0 0 1 0

to:
_
0 0 0 1 0
1 1 1 0 1
1 1 1 0 1
0 0 0 1 0

I think I have cracked it right there? Is this what you mean by flagging?

edit: well four questions in a way before anyone says owt :slight_smile:

SOLVED :slight_smile:

Used a method:

grid.setTile(4, 8, TileType.water);

public void setTile(int xCoord, int yCoord, TileType type) {
		map[xCoord][yCoord] = new Tile(xCoord * 64, yCoord * 64, 64 , 64, type);
	}