2

I'm trying to make a function to check if a cell in Tiled can be passed, by accessing a Boolean custom property I gave each tile. This is (part of) my code.

...
public boolean isCellPassable(int column, int row, MapLayer layer) {
    boolean canPass = Boolean.valueOf((Boolean) ((TiledMapTileLayer) layer).getCell(column, row).getTile().getProperties().get("can_pass"));
    if (canPass == true) {
        return true;
    }
    else {
        return false;
    }
}

public void displayHUD(ShapeRenderer hud) {
    System.out.println(isCellPassable(0, 6, Main.level1.getLayers().get("base")));
...

And even though in the isCellPassable function I cast it to a Boolean, for some reason I still get this error ..

Exception in thread "LWJGL Application" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

.. on the line where I set the value of canPass.

Jacob_
  • 247
  • 2
  • 13

1 Answers1

0

You should probably remove the cast to Boolean, and make sure the Boolean.valueOf(String) overload is chosen.

Thorbjørn Lindeijer
  • 2,022
  • 1
  • 17
  • 22
  • When I do this, eclipse gives me an error and suggests I add the cast to Boolean. It tells me this: `The method valueOf(boolean) in the type Boolean is not applicable for the arguments (Object)` @Thorbjørn Lindeijer – Jacob_ Apr 07 '16 at 16:31
  • this is the code after I followed your answer: `boolean canPass = Boolean.valueOf(((TiledMapTileLayer) layer).getCell(column, row).getTile().getProperties().get("can_pass"));` – Jacob_ Apr 07 '16 at 16:32
  • Hmm, I expected the `get` function in libgdx `MapProperties` class to return a `String` based on the title of your question. But actually it returns an `Object`. So, the `Boolean.valueOf(String)` overload isn't chosen. You should probably check the type of the returned object and cast it to a `String` if applicable. – Thorbjørn Lindeijer Apr 10 '16 at 21:04