Search code examples
dictionarypropertieslibgdxtiled

Tiled MapObject size


I am loading Tiled map via TmxMapLoader and I have no trouble getting tiles, etc. But when I want to extract values from Object Layer, I am only able to get x, y with this code:

MapProperties props = layer.getObjects().get(i).getProperties();
float x = (float) props.get("x");
float y = (float) props.get("y");

Which gets me the right values, but when I try to add this:

float width = (float) props.get("width");
float height = (float) props.get("height");

It throws me this error:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at cz.vilix.managers.MapManager.<init>(MapManager.java:67)
at cz.vilix.main.Game.create(Game.java:50)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

(Line 67 in MapManager is where I try to get the width value.) It seems a bit ood to me, because I though, that I can get anything from the properties of an object, which is listed in the properties window:

enter image description here

Also using "Width" & "Height" instead of "width" & "height" doesn't work.

Changing the type to Object like this:

Object width = props.get("Width");
Object height = props.get("Height");
System.out.println(width + " " + height);

Prints: null null.


Solution

  • The solution that works is to get RectangleMapObject (or any other shape), and the get the basic Rectangle with this code: Rectangle r = rect.getRectangle() and the you can use parameters of the Rectangle.

    Or you can skip the step of creating a new Rectangle object and use it as in this code:

    RectangleMapObject rect = (RectangleMapObject) layer.getObjects().get(i);
    float x = (float) rect.getRectangle().x;
    float y = (float) rect.getRectangle().y;
    float width = rect.getRectangle().width;
    float height = rect.getRectangle().height;