在Libgdx中删除Tiles(java)

时间:2017-06-21 16:05:30

标签: java libgdx tile

美好的一天!

我需要Libgdx项目的帮助。 我想要的是删除我的图形层(第0层)中的图块。

这是我到目前为止所得到的:

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);
      }

投币体和getCells方法:

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));
}

除了一件事之外,似乎工作得非常好:

蓝币被击中:

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

绿色硬币被击中:

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

红色硬币被击中: 不能发布超过两张图片:(。 红色硬币消失了,一切都很好。但是,只有我一个接一个地打他们。所以我的意思是顺序:Redcoin,Greencoin,Bluecoin

我注意到一些非常奇怪的东西:

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

但:

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

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

很难从中诊断出确切的问题,但对我来说最突出的是你使用==运算符进行字符串比较。

如果要测试两个字符串(或Java中的任何Object类型)的相等性,则应使用String.equals方法执行此操作:

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

equals方法检查对象的值是否相同。 ==运算符检查对象的引用是否相同,并且在与字符串文字进行比较时具有不可预测的行为。