How to read polyline cords from a *.tmx file?

476 views Asked by At

I am using TmxMapLoader and I can't seem to find a way to read polyline data from a *.tmx.

TmxMapLoader mapLoader = new TmxMapLoader();
TiledMap map = mapLoader.load("map1.tmx");      
MapLayers layers = map.getLayers();     
Iterator<MapLayer> layersIter = layers.iterator();      
while(layersIter.hasNext()) {
    MapLayer layer = layersIter.next();
    if(layer.getName().equals("path")) {
        MapObjects os = layer.getObjects();
        Iterator<MapObject> osIter = os.iterator();
        while(osIter.hasNext()) {
            MapObject o = osIter.next();
            MapProperties p = o.getProperties();
            // p.get("x") p.get("y") - <object x="" y""> works just fine
            // but how can I get all polyline data from <polyline>?
        }
    }
}

Relevant part of *.tmx file:

<objectgroup color="#9da0a4" name="path">
    <object x="9.09091" y="1509.09">
        <polyline points="0,0 1,1"/>
    </object>
</objectgroup>

I checked the code for TmxMapLoader and it seems that it has this functionality implemented, yet I can't find a way to get it.

Any ideas?

1

There are 1 answers

1
vzamanillo On BEST ANSWER

You can get the Polyline as follows, where o is a MapObject:

Polyline polyline = ((PolylineMapObject)o).getPolyline();

Keep in mind that you may have check the instance before to prevent a ClassCastException

if(o instanceof PolylineMapObject) {
    Polyline polyline = ((PolylineMapObject)object).getPolyline();
    .....
}

Hope this helps.