LibGDX在我的Tiled对象层中未检测到对象。

huangapple go评论69阅读模式
英文:

LibGDX is not detecting objects in my Tiled object layer

问题

我正在开发一个使用LibGDX的简单的自顶向下游戏,并且目前正在尝试实现碰撞。我正在使用从Tiled编辑器导入的对象,但出现一个问题,它似乎无法识别对象图层中的对象。当我尝试获取该图层中的对象数量时,它只返回0。

我目前的理论是它可能没有正确调用对象组,但我完全找不到解决方案。我已经尝试解决这个问题很长时间,唯一确定的问题是代码无法在该图层中找到任何对象。如果有任何帮助或建议,将不胜感激。这是我遇到问题的部分。

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

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());

答案1

得分: 1

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.

英文:

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.

huangapple
  • 本文由 发表于 2023年5月25日 03:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定