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

标签 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 设置为在与 MotherCellEntity 开始 Contact 时执行以下代码:

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

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

最佳答案

我是如何成功的

我所做的是在我的 GameStateRegular 类中的 update 方法中:

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

关于java - 引用 World 时表达式 : IsLocked() == false, 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512183/

相关文章:

java - HashMap 是最好的选择吗?

java - 从另一个类 Android Studio 访问变量

java - JVM 在 Solaris 机器中崩溃 有问题的帧 : # C [libc. so.1]# [ 计时器已过期,中止...]

Android Parse 没有收到推送通知

c++ - 我的 .exe 在 Qt Creator 中运行后立即崩溃

c++ - 如何基于 b2Joint 实例提取/创建 JointDef

javascript - Box2D body 在碰撞时不旋转

java - 使用 Spring Data JPA 的自定义查询返回自定义对象

c++ - 如何在 C++ 中将 `.a` 文件导入 CMake?

java - 关闭 selenium java 中的弹出窗口