[SOLVED] LibGDX Tilemap Null Tiles

For reference, I am following this source:

Hi there!

I am having a bit of an issue at the moment. If you take a look at that source link, you will see a method titled “getTiles” with several parameters. I am doing this exact same thing for a collision practice thing. The issue I am having is that the cells are returning null, so the tiles are never actually added or I guess “registered” in this case. The (if cell != null) never gets to run.

I am using the Tiled program for the tilemap and have textures on a layer, so I’m not sure what the issue is. Do I need to tick some setting for it to no longer return null?

Thanks!

  • A

You’ll get null if the cell is not populated, are you sure you’re calling it with valid coordinates?
Are you sure you’re doing it on the correct layer?

Can you show some of your code?

Sure thing!

	private void getTiles (int startX, int startY, int endX, int endY, Array<Rectangle> tiles) {
		TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(1);
		rectPool.freeAll(tiles);
		tiles.clear();
		for (int y = startY; y <= endY; y++) {
			for (int x = startX; x <= endX; x++) {
				Cell cell = layer.getCell(x, y);
				if (cell != null) {
					Rectangle rect = rectPool.obtain();
					rect.set(x, y, 1, 1);
					tiles.add(rect);
				}
			}
		}
	}
   
map.getLayers().get(1) 

Off-By-One? Are you sure you don’t want get(0) ?

I fixed the issue by rewriting the entire thing. I’m assuming that the problem was just that camera/tilemap scaling was not linking up correctly and it wasn’t detecting any tiles near the coordinates. Now I’m trying to figure out to efficiently setup camera culling… Any ideas on that?

Just remember to add [Solved] at the start of the title of the topic :stuck_out_tongue: