Libgdx Box2d Java Crash - jniCreateBody

时间:2015-08-10 09:57:13

标签: java crash libgdx box2d

我现在和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$1.run()V+27
v  ~StubRoutines::call_stub

我做什么: 我偶尔创造一个身体。大多数时候一切都还好,但有时候仍会崩溃。第一次创作直到现在都没有失败。

  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

的创建

例如,我有几乎相同的对象,没有崩溃

创造是蜜蜂创造之前的一行

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;
}

也没有帮助。

100%没有在另一个对象上创建正文。

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

请尽力帮助我!

1 个答案:

答案 0 :(得分:3)

我意识到,我可以在几秒钟内产生数百万只蜜蜂,没有任何问题,所以我每秒创造大约10只。没有崩溃,直到箭击中Boss并被摧毁 - 游戏崩溃。

我之前看过一些被破坏的身体修改,但我错过了一行...

问题是: 我从被毁坏的身体中设定了速度!

直到现在才崩溃。

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

“当你摧毁一具尸体时,请: body = null 这将帮助你找到你的错,如果不是你可能会得到一些难以理解的错误“

希望你能从中得到帮助。