【问题标题】:LibGDX Tiled get object property on collisionLibGDX Tiled 在碰撞时获取对象属性
【发布时间】:2018-04-25 10:45:37
【问题描述】:

我正在 LibGDX 中开发一款游戏,并使用 Tiled 设置了一张地图。我为特定层中的对象添加了一个自定义 String 属性,以获取更多信息,它代表什么对象。

我设置了一个 ContactListener,它调用地图对象抽象类中的一个方法。监听器看起来像这样:

@Override
public void beginContact(Contact contact) {

    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();

    if (fixtureA.getUserData() == "player" || fixtureB.getUserData() == "player") {
        // Get either fixture A or B, depending on which of them is the player fixture
        Fixture player = fixtureA.getUserData() == "player" ? fixtureA : fixtureB;
        // Get the colliding object, depending on which of them is the player fixture
        Fixture collidedObject = player == fixtureA ? fixtureB : fixtureA;

        // Determine what kind of object the player collided with and trigger the respectable method
        if (collidedObject.getUserData() instanceof InteractiveMapTileObject) {
            ((InteractiveMapTileObject) collidedObject.getUserData()).onPlayerBeginContact();
        } else if (collidedObject.getUserData() instanceof Enemy) {
            ((Enemy) collidedObject.getUserData()).onPlayerBeginContact();
        }
    }
}

当玩家点击 InteractiveMapTileObject 实例的对象时,会调用 onPlayerBeginContact() 方法,如下所示:

@Override
public void onPlayerBeginContact() {
    MapObjects objects = playScreen.getMap().getLayers().get("weapon").getObjects();
    for (MapObject object : objects) {
        if (object.getProperties().containsKey("weapon_name")) {
            String weaponName = object.getProperties().get("weapon_name", String.class);
            Gdx.app.log("Picked up weapon", weaponName);
        }
    }
}

在这里,我正在获取地图中“武器”图层的对象,然后对其进行迭代以找到正确的属性及其值。这工作正常。

现在的问题是,我显然在图层中有多个对象,因此有多个 MapObjects。我需要一种方法来识别玩家碰撞的对象,然后获取它的属性。

是否可以使用 ContactListener 做到这一点,还是我需要实现其他东西?我已经搜索了很多帖子,但没有走运。

【问题讨论】:

    标签: java libgdx collision-detection tiled


    【解决方案1】:

    我认为与其从 MapObject 获取武器名称,不如从 Weapon 类获取它。

    每个武器都可以继承InteractiveTileMapObject。当玩家与夹具发生碰撞时,您可以从用户数据中获取参考,并使用参考获取武器名称。

    我希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!

    【讨论】:

    • 我尝试评估用户数据,当与对象碰撞时,我无法识别碰撞对象属性,我只能访问地图属性和以前的所有内容。这是用户数据的截图:imgur.com/a/GxJ2R
    【解决方案2】:

    我通过在我的抽象类中编写一个自定义方法来检查特定对象层中每个对象的交集,从而解决了我的问题。

    protected MapObject getCellProperties(int layerIndex) {
        MapObjects mapObjects = map.getLayers().get(layerIndex).getObjects();
    
        for (MapObject mapObject : mapObjects) {
            MapProperties mapProperties = mapObject.getProperties();
    
            float width, height, x, y;
            Rectangle objectRectangle = new Rectangle();
            Rectangle playerRectangle = new Rectangle();
    
            if (mapProperties.containsKey("width") && mapProperties.containsKey("height") && mapProperties.containsKey("x") && mapProperties.containsKey("y")) {
                width = (float) mapProperties.get("width");
                height = (float) mapProperties.get("height");
                x = (float) mapProperties.get("x");
                y = (float) mapProperties.get("y");
                objectRectangle.set(x, y, width, height);
            }
    
            playerRectangle.set(
                    playScreen.getPlayer().getX() * MainGameClass.PPM,
                    playScreen.getPlayer().getY() * MainGameClass.PPM,
                    playScreen.getPlayer().getWidth() * MainGameClass.PPM,
                    playScreen.getPlayer().getHeight() * MainGameClass.PPM
            );
    
            // If the player rectangle and the object rectangle is colliding, return the object
            if (Intersector.overlaps(objectRectangle, playerRectangle)) {
                return mapObject;
            }
        }
    
        // If no colliding object was found in that layer
        return null;
    }
    

    这可能不是最好的解决方案,但效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 2017-12-19
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多