Libgdx Box2d Java 崩溃 - jniCreateBody

Libgdx Box2d Java Crash - jniCreateBody

我玩 Libgdx 已经有一段时间了,但第一次遇到这个问题。

我的问题:当我尝试创建 Body 时,Java 环境崩溃了。 错误日志:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) 
    j com.badlogic.gdx.physics.box2d.World.jniCreateBody(JIFFFFFFFFZZZZZF)J+0
j  com.badlogic.gdx.physics.box2d.World.createBody(Lcom/badlogic/gdx/physics/box2d/BodyDef;)Lcom/badlogic/gdx/physics/box2d/Body;+80
j  xxx.xx.box2dTest.Enteties.SmallBee.init(FF)V+60
j  xxx.xx.box2dTest.Enteties.SmallBee.<init>(Lcom/badlogic/gdx/physics/box2d/World;Lcom/badlogic/gdx/graphics/OrthographicCamera;)V+112
J 1268 C1 xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOneWorld.spawnSmallBee()V (48 bytes) @ 0x000000000323e174 [0x000000000323dec0+0x2b4]
j  xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOne.extraStuff()V+223
J 1176 C1 xxx.xx.box2dTest.Organization.Ingame.Level.MotherLevel.render(F)V (269 bytes) @ 0x00000000031e1a0c [0x00000000031e0460+0x15ac]
J 1175 C1 xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOne.render(F)V (499 bytes) @ 0x00000000031d994c [0x00000000031d98c0+0x8c]
J 1144 C1xxx.xx.box2dTest.Box2DGame.render()V (5 bytes) @ 0x00000000031cb744 [0x00000000031cb520+0x224]
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V+684
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication.run()V+27
v  ~StubRoutines::call_stub

我的工作: 我时不时地创建一个 body。大部分时间都还好,偶尔还是会崩溃。。第一次创建到现在都没有失败过。

  public void spawnSmallBee(){
    if(TimeUtils.millis() - smallBeeTimer > 0){
        new SmallBee(getWorld(), getCamera());
        smallBeeTimer = TimeUtils.millis() + MathUtils.random(2000, 3000);
    }
}

此代码片段是我创建 Object 的地方,它将创建 Body

public SmallBee(World world, OrthographicCamera camera) {
    this.world = world;
    this.camera = camera;
    MOVING_SPEED_Y= MathUtils.random(-3, 3);

    sprite = new Sprite(MyAssets.SMALL_BEE);
    sprite.setSize(WIDTH * 2, HEIGHT * 2);
    sprite.setOriginCenter();
    myEntity = new MyEntity("Bee", sprite);
    myEntity.setObject(this);

    float posx = camera.position.x - camera.viewportWidth / 2 - 2;
    float posy = MathUtils.random(1, 7);

    BodyDef platformDef = new BodyDef();
    platformDef.type = BodyDef.BodyType.KinematicBody;
    platformDef.position.set(posx, posy);
    body = world.createBody(platformDef);
    PolygonShape platformBox = new PolygonShape();
    platformBox.setAsBox(WIDTH, HEIGHT);

    body.createFixture(platformBox, 0.0f);

    body.setLinearVelocity(new Vector2(MOVING_SPEED_X, MOVING_SPEED_Y));
    myEntity.getSprite().setPosition(body.getPosition().x - WIDTH, body.getPosition().y - HEIGHT);
    body.setUserData(myEntity);
    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    platformBox.dispose(); }

此代码片段是 Object

的创建

例如,我得到了几乎相同的 Object,但没有崩溃

创造是蜜蜂创造前的一行

public void spawnArrowShooter() {
    if(TimeUtils.millis() - arrowShooterTimer > 0){
        new ArrowShooter(getWorld(), MathUtils.random(200 - getCamera().viewportWidth / 2 + 2, 200 + getCamera().viewportWidth / 2 - 2), 3, getPlayer(), getCamera(), bossWasp);
        arrowShooterTimer = TimeUtils.millis() + MathUtils.random(10000, 15000);
    }
}

启蒙:

 public ArrowShooter(World world, float x, float y, Player player, OrthographicCamera camera, BossWasp bossWasp) {
    this.camera = camera;
    this.bossWasp = bossWasp;
    //PLATTFORM
    this.world = world;
    this.player = player;

    sprite = new Sprite(MyAssets.ARROW);
    sprite.setSize(WIDTH * 2, HEIGHT * 2);

    myEntity = new MyEntity("Arrow", sprite);
    myEntity.setObject(this);



    init(x, y);

}


 public void init(float x, float y){
    BodyDef platformDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesn't move we would set it to StaticBody
    platformDef.type = BodyDef.BodyType.DynamicBody;

    platformDef.position.set(x, y);
    body = world.createBody(platformDef);
    PolygonShape platformBox = new PolygonShape();
    platformBox.setAsBox(WIDTH, HEIGHT);
    Fixture fixture = body.createFixture(platformBox, 0.0f);
    myEntity.getSprite().setPosition(body.getPosition().x - WIDTH, body.getPosition().y - HEIGHT);

    body.setUserData(myEntity);
    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    platformBox.dispose();
    disappearTime = TimeUtils.millis() + 4000;
}

我想不通问题..

if(world.isLocked()){
  return;
}

也无济于事。

body 100% 不是在另一个 Object 上创建的。

我会尝试进一步调查问题,并提供我可以收集到的任何新信息。

请帮帮我!

我意识到,我可以在几秒钟内毫无问题地生成数百万只蜜蜂,所以我每秒创建大约 10 个。没有崩溃,直到箭头击中 Boss 并被摧毁 - 立即游戏崩溃。

我早些时候看到了一些被破坏的 body 修改,但我错过了一行..

问题是: 我已经从被摧毁的 body!

中设置了速度

到现在为止没有再发生崩溃。

正如我在另一个论坛上看到的那样:

“当你销毁 body 时,请执行以下操作: body = 空 这将帮助您找到错误,否则您可能会遇到一些难以理解的错误

希望能从中得到帮助。