表达式:IsLocked()== false,引用World时崩溃?

时间:2015-02-14 03:42:48

标签: java crash box2d

错误

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jre1.8.0_25\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Dynamics/b2World.cpp, Line 109

Expression: IsLocked() == false

以下行是错误发生前的最后一行:

Body body = world.createBody(bodyDef);

我不明白这是如何导致问题的。 worldbodyDef不是null

我的程序是如何构建的,我有一个GameStateRegular类,在init方法中,它添加了两个新的VirusEntity对象,如下所示:

VirusEntity virus0 = new VirusEntity(world, mother, new Vector2(0, 0));
VirusEntity virus1 = new VirusEntity(world, mother, new Vector2(100, 100));
viruses.add(virus0);
viruses.add(virus1);

这很好用。我有一个VirusEntity设置,以便在Contact开始MotherCellEntity时执行以下代码:

private void setContactListener() {
    world.setContactListener(new ContactAdapter() {
        @Override
        public void beginContact(Contact contact) {
            Body bodyA = contact.getFixtureA().getBody();
            Body bodyB = contact.getFixtureB().getBody();

            if (bodyA == mother.getBody()) work(bodyA, bodyB);
            else if (bodyB == mother.getBody()) work(bodyB, bodyA);
        }

        private void work(Body motherCellBody, Body virusBody) {
            VirusEntity virus = null;
            for (VirusEntity v : viruses) {
                if (v.getBody() == virusBody) virus = v;
            }
            virus.remove();
            VirusEntity newVirus = new VirusEntity(world, mother, virus.getSpawnPosition());
            viruses.add(newVirus);
        }
    });
}

我正在引用相同的World对象,以及与正在创建的新MotherCellEntity相同的VirusEntity对象,但我仍然不知道这里发生了什么。

完整GameStateRegular课程:http://pastebin.com/mFpmp28T
完整VirusEntity课程:http://pastebin.com/HjSGS4ER

先谢谢大家提供任何帮助。我对这个话题非常模糊,因为我真的不明白发生了什么。我试图对此进行一些背景研究,但我没有成功。

1 个答案:

答案 0 :(得分:0)

我如何成功

我所做的是在update课程中的GameStateRegular方法内部:

@Override
public void update() {
    world.step(1, 1, 1);
    mother.update();
    for (int i = 0; i < viruses.size(); i++) {
        VirusEntity e = viruses.get(i);
        if (e.isRemoved()) {
            viruses.remove(e);
            e.dispose();
            // --v--
            VirusEntity newVirus = new VirusEntity(world, mother, e.getSpawnPosition());
            viruses.add(newVirus);
            // --^--
        } else e.update();
    }
}

另外,对于我的ContactAdapter

private void setContactListener() {
    world.setContactListener(new ContactAdapter() {

        @Override
        public void preSolve(Contact contact, Manifold manifold) {
            Body bodyA = contact.getFixtureA().getBody();
            Body bodyB = contact.getFixtureB().getBody();

            if (bodyA == mother.getBody()) virusInvolved(bodyA, bodyB);
            else if (bodyB == mother.getBody()) virusInvolved(bodyB, bodyA);
        }

        private void virusInvolved(Body motherCellBody, Body virusBody) {
            VirusEntity virus = null;
            for (VirusEntity v : viruses) {
                if (v.getBody() == virusBody) virus = v;
            }
            if (virus != null) {
                virus.setMovementAllowed(false);
                virus.remove();
            }
        }
    });
}