android - LibGDX Touch Box2D 体

标签 android box2d libgdx

我正在使用 LibGDX 创建一个新项目。

我想做的是,我将 tmx 文件中的主体加载到工作正常的关卡中。尸体也有一个 Sprite 。

问题是,我想让用户触摸场景中的某些 body 。当他们触摸 body 时,他们将能够将其删除或从场景中移除。

我不太熟悉在 libgdx 中做这样的事情。虽然我确信它没有那么复杂。

无论如何我可以在 LibGDX 中做到这一点吗?

编辑:

这是我目前所拥有的。

QueryCallback callback = new QueryCallback() {


    @Override
    public boolean reportFixture(Fixture fixture) {
        // if the hit fixture's body is the ground body
        // we ignore it

        // if the hit point is inside the fixture of the body
        // we report it

        hitBody = fixture.getBody();

        return true;
    }
};

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    hitBody = null;


    return false;
}

现在我只是不确定如何删除被点击的主体..

最佳答案

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java 使用此链接使用 queryAABB 选择具有接触点的对象。此代码还提供了如何使用鼠标关节移动对象。如果要删除对象,请确保在世界步循环后删除它们。

编辑:

....
public boolean touchDown (int x, int y, int pointer, int newParam) {
        testPoint.set(x, y, 0);
        camera.unproject(testPoint);
        hitBody = null;
        world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);

        return false;
}
.....

关于android - LibGDX Touch Box2D 体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092023/

相关文章:

android - 如何知道 fragment 中的哪个元素/ View 具有焦点

iphone - 无法从 C++ 文件访问对象属性 (Cocos2D/Box2d)

cocos2d-iphone - 如何给 Box2d Kinematic body 一个预定义的曲线路径

json - 如何使用 LibGDX 解析这个 JSON

java - Android ListView 未更新

android - 是否可以建立内部 APNs 服务器?

java - 具有多个可拉伸(stretch)区域的 Libgdx 9 补丁

java - 纹理透明度在 libgdx 中不起作用

java - 如何让我的 AlertDialog 以等宽字体显示其文本?

ios - 单面平台 (Box2D/Cocos2D)