javaobjectlibgdxtiled

LibGDX is not detecting objects in my Tiled object layer


I'm working on a simple top-down game in LibGDX right now and I'm currently trying to implement collisions. I'm using objects imported from the Tiled editor, but for some reason it won't register the objects in my object layer. When I try to get a count for the objects in it, it just returns 0.

My working theory is its somehow not calling the object group like it should but I'm completely lost for a solution. I've been trying to fix the issue for ages and the only thing I've managed to do is confirm that the problem is that the code can't find any objects in the layer Any help or advice would be much appreciated. this is the section I am having trouble with.

int objectLayerId = 2;
TiledMapTileLayer collisionObjectLayer = (TiledMapTileLayer)tiledMap.getLayers().get(objectLayerId);
Gdx.app.log("Count", "Object count:"+ collisionObjectLayer.getObjects().getCount());

Solution

  • So, after much struggling, research, and then more struggling, I have discovered the solution. It is frustratingly simple. All I needed to do was change my code to the following:

    StaticObjects = new MapObjects();
        StaticObjects = tiledMap.getLayers().get("CollisionLayer").getObjects();
        Gdx.app.log("Count", "Object count:" +StaticObjects.getCount());
    

    From what I can tell, the issue was that object layers can't be assigned as TiledMapTileLayer.