Vepir
Vepir

Reputation: 602

MapObject in libGDX+Tiled : Set the object properties, not the custom object properties?

I load the TiledMap map, then load all the MapObjects to MapObject[] npc,

I want to copy one of the objects to a different place on the second tiled map.

npcs[0].getProperties().put("X", 0);
npcs[0].getProperties().put("Y", 0);
secondMap.getLayers().get("NPC").getObjects().add(npcs[0]);

The object gets placed, but the first two lines do not change the X and Y property to 0, instead, they place custom properties X and Y, as I believe.

enter image description here

This is what I suspect happens instead. How would I edit object properties, not custom?

Upvotes: 0

Views: 458

Answers (1)

Vepir
Vepir

Reputation: 602

Turns out you need to make a cast, depending on your object type, to access the objects properties; My objects being each a 64x64 rectangle;

((RectangleMapObject) npcs[0]).getRectangle().setX(0);
((RectangleMapObject) npcs[0]).getRectangle().setY(0);
secondMap.getLayers().get("NPC").getObjects().add(npcs[0]);

But this will only reference the same npc[0], and if we try to add more than one copy, it will place all copies to the place of the last copy;

Meaning for each new copy, we need to create a new instance of the object and copy its properties, and then simply edit the different properties; coordinates in my case:

RectangleMapObject object = new RectangleMapObject();
object.getProperties().putAll(npcs[0].getProperties());
object.getRectangle().set(0,0,64,64);
secondMap.getLayers().get("NPC").getObjects().add(object);

This is how I managed to solve (understand) it in the end.

Upvotes: 1

Related Questions