LibGDX ClassCastException

Hi, really quick and hopefully simple question here. I want to load in the map properties for this .tmx file I have, and I’m doing it like this:


playerSpawn = new Vector2(getLevel().getProperties().get("player spawn x", Float.class), (float)getLevel().getProperties().get("player spawn y", Float.class));

This is causing a ClassCastException, “java.lang.String cannot be cast to java.lang.Float”

Why would it throw a ClassCastException when I’m specifying that it’s a float? And which part of it is a string? I thought TiledMap.getProperties().get returns an Object?

Any ideas?

Maybe you put a String value in properties somewhere?

I don’t understand why you are retrieving the player’s coordinates like that. Try to build a class for the player and update his location whenever he moves. When you want to know his current position, just set getters to his class.

Uhh those aren’t the player’s coordinates. It’s just the player’s starting position (spawn) for that level. I thought each level would have a different starting position so therefore I can store it as a map property and retrieve it whenever I’m transitioning to a new level.

Here’s exactly what I’m doing:

  1. Creating a level in Tiled
  2. Creating map properties for that level called “player spawn x” and “player spawn y”
  3. Putting numbers in for the values of those two properties (not strings!)
  4. Loading those properties in via the technique above.

So why does it think it’s a string?

I think that the level properties might automatically be set to be strings, so perhaps try making it

Float.parseFloat(getLevel.getProperties().get("player spawn x"))

and the same thing for y;

Yeah that worked. I’m not sure why you have to do it like that when on LibGDX wiki they don’t have to cast it in their examples.

You can find that wiki page here: https://github.com/libgdx/libgdx/wiki/Tile-maps

Oh well, at least it’s working now. Thank you.