0

Good Day!

I Need help with a Libgdx Project. What i want is to remove a tile, which is in my graphics layer (layer 0).

This is what i got so far:

if(object1.getUserData() != null && object1.getUserData() == "polyblock"){
       polyblock1 = true;
       System.out.println("PLAYER 1");
}else if(object1.getUserData() != null && object1.getUserData() == "redcoin" || object1.getUserData() == "greencoin" ||  object1.getUserData() == "bluecoin" ){
           B2dObjectGetter.getCell(object1.getUserData() == "redcoin" ? 3 : object1.getUserData() == "greencoin" ? 4: 5).setTile(null);
      }

The coin bodies and the getCells Method:

for (int i = 3; i <= 5; i++){
     for (MapObject obj : map.getLayers().get(i).getObjects().getByType(EllipseMapObject.class)) {
            Ellipse ellipse = ((EllipseMapObject) obj).getEllipse();

            bdef.type = BodyDef.BodyType.StaticBody;
            bdef.position.set((ellipse.x + ellipse.width / 2) / GameJLibGDX.PPM, (ellipse.y + ellipse.height / 2) / GameJLibGDX.PPM);

            CircleShape shape = new CircleShape();

            coinbodies[i] = world.createBody(bdef);
            shape.setRadius(ellipse.width / 2 / GameJLibGDX.PPM);
            fixdef.shape = shape;
            coinbodies[i].createFixture(fixdef).setUserData(i == 3 ? "redcoin" : i == 4 ? "greencoin" : "bluecoin");
        }
    }

}

public static TiledMapTileLayer.Cell getCell(int i){
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    System.out.println(coinbodies[i].getFixtureList());
    return layer.getCell((int)(coinbodies[i].getPosition().x * GameJLibGDX.PPM / 128),(int)(coinbodies[i].getPosition().y*GameJLibGDX.PPM / 128));
}

It seems to work really fine except one Thing:

Blue coin got hit:

The tiles of the other two coins move instead of disappearing like the blue one

Green coin got hit:

The green coin is this appearing but the red coin tile still switches

Red coin got hit: Cant post more than two Pictures :(. The red coin is disappering and everything is fine. But only if I hit them one after another. So i mean in the order: Redcoin, Greencoin, Bluecoin

I noticed something really weird:

As normal i hit it and it moved so its still bugged

but:

When i move to the right in the Moment the Picture would go outside the Screen, because the camera Position is attached to the Player, the coin moves back to its right position

Thank you for your time!

Jakob
  • 1
  • 2

1 Answers1

0

It's hard to diagnose the exact problem from this but the thing that stands out to me is your use of the == operator for String comparison.

If you want to test the equality of two Strings (or any Object type in Java) you should do it with the String.equals method like this:

object1.getUserData().equals("my string")

The equals method checks that the values of the objects are the same. The == operator checks that the references to the object are the same and has unpredictable behaviour when you are comparing against a string literal.