简体   繁体   English

崩溃:com.badlogic.gdx.physics.box2d.World.jniCreateBody

[英]CRASH: com.badlogic.gdx.physics.box2d.World.jniCreateBody

I get a EXCEPTION_ACCESS_VIOLATION crash from the java runtime enviroment when trying out my game.尝试我的游戏时,我从 java 运行时环境中收到 EXCEPTION_ACCESS_VIOLATION 崩溃。 It is written with LibGdx (and uses box2d).它是用 LibGdx 编写的(并使用 box2d)。 It is running on desktop mode from android studio.它在 android studio 的桌面模式下运行。

I added a "fireball" function to my supermario game, and get this error when jumping into air and shooting fireballs.我在我的超级马里奥游戏中添加了“火球”功能,并且在跳入空中射击火球时出现此错误。 Here is the crash log:这是崩溃日志:

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 com.mygdx.game.sprite.Fireball.define()V+68 j com.mygdx.game.sprite.Fireball.(Lcom/mygdx/game/screen/PlayScreen;FFZ)V+135 j com.mygdx.game.sprite.Mario.shootFire()V+36 J 1115 C1 com.mygdx.game.screen.PlayScreen.handleInput(F)V (221 bytes) @ 0x000000000320f90c [0x000000000320e960+0xfac] J 1073 C1 com.mygdx.game.screen.PlayScreen.update(F)V (188 bytes) @ 0x00000000031e14ec [0x00000000031e1440+0xac] J 1074 C1 com.mygdx.game.screen.PlayScreen.render(F)V (252 bytes) @ 0x00000000031e493c [0x00000000031e3f20+0xa1c] J 1223 C1 com.mygdx.game.MarioBros.render()V (5 bytes) @ 0x0000000003263ae4 [0x0000000003263920+0x1c4] j com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V+698 j com.badlogic.gdx.backends.lwjgl.LwjglApplic Java 框架:(J=编译的 Java 代码,j=解释的,Vv=VM 代码)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 com.mygdx.game.sprite.Fireball.define()V+68 j com .mygdx.game.sprite.Fireball.(Lcom/mygdx/game/screen/PlayScreen;FFZ)V+135 j com.mygdx.game.sprite.Mario.shootFire()V+36 J 1115 C1 com.mygdx.game .screen.PlayScreen.handleInput(F)V(221个字节)@ 0x000000000320f90c [0x000000000320e960 + 0xfac:J 1073 C1 com.mygdx.game.screen.PlayScreen.update(F)V(188个字节)@ 0x00000000031e14ec [0x00000000031e1440 + 0xac] J 1074 C1 com.mygdx.game.screen.PlayScreen.render(F)V(252 字节)@ 0x00000000031e493c [0x00000000031e3f20+0xa1c] J 1223 C1 com.mygdxr0x0x30gamebytes(252 字节)(252 字节)@0x0000000031e493c [0x0000000003263920+0x1c4] j com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V+698 j com.badlogic.gdx.backends.lwjgl.LwjglApplic ation$1.run()V+27 v ~StubRoutines::call_stub ation$1.run()V+27 v ~StubRoutines::call_stub

Here is my Fireball class:这是我的火球课:

public class Fireball extends Sprite {

private PlayScreen playScreen;
private World world;
private Array<TextureRegion> frames;
private Animation<TextureRegion> animation;
private float stateTimer;
private boolean destroyed;
private boolean destroy;
private boolean fireToRight;
private Body body;

public Fireball(PlayScreen playScreen, float x, float y, boolean fireToRight){
    this.playScreen = playScreen;
    this.fireToRight = fireToRight;
    this.world = playScreen.getWorld();
    destroy = false;
    destroyed = false;
    frames = new Array<TextureRegion>();
    for(int i = 0; i < 4; i++)
        frames.add(new TextureRegion(playScreen.getAtlas().findRegion("fireball"),i*8,0,8,8));
    animation = new Animation<TextureRegion>(0.2f, frames);
    setRegion(animation.getKeyFrame(0));
    setBounds(x, y, 6/ C.PIXEL_PER_METER, 6/C.PIXEL_PER_METER);
    define();
}

private void define(){
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(fireToRight ? getX() + 12 /C.PIXEL_PER_METER : getX() - 12 /C.PIXEL_PER_METER, getY());
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    if(world.isLocked())
        return;
    body = world.createBody(bodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(3 / C.PIXEL_PER_METER);
    fixtureDef.filter.categoryBits = C.FIREBALL_BIT;
    fixtureDef.filter.maskBits = C.GROUND_BIT |
            C.COIN_BIT |
            C.BRICK_BIT |
            C.ENEMY_BIT |
            C.OBJECT_BIT;
    fixtureDef.shape = shape;
    fixtureDef.restitution = 1;
    fixtureDef.friction = 0;
    body.createFixture(fixtureDef).setUserData(this);
    body.setLinearVelocity(new Vector2(fireToRight ? 2 : -2, 2.5f));
}

public void update(float deltaTime){
    if((stateTimer > 3 || destroy) && !destroyed){
        world.destroyBody(body);
        destroyed = true;
        body = null;
        return;
    }

    stateTimer += deltaTime;
    setRegion(animation.getKeyFrame(stateTimer, true));
    setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2);

    if(body.getLinearVelocity().y > 2f)
        body.setLinearVelocity(body.getLinearVelocity().x,2f);
    if((fireToRight && body.getLinearVelocity().x < 0 ) || (!fireToRight && body.getLinearVelocity().x > 0))
        destroy();
}

public void destroy(){
    destroy = true;
}

public boolean isDestroyed(){
    return destroyed;
}

} }

I call mario.shootFire() from PlayScreen class.我从 PlayScreen 类调用mario.shootFire()

This is how "mario shoots fireballs":这就是“马里奥射击火球”的方式:

private Array<Fireball> fireballs;

fireballs = new Array<Fireball>();

 public void shootFire(){
        fireballs.add(new Fireball(playScreen, body.getPosition().x, body.getPosition().y, runRight));
    }

    @Override
    public void draw(Batch batch) {
        super.draw(batch);
        for (Fireball fireball: fireballs)
            fireball.draw(batch);
    }

Any ideas on why i get this error?关于为什么我收到此错误的任何想法?

EDIT: It seems like it crashes at this line body = world.createBody(bodyDef) in define() method in Fireball.编辑:它似乎在 Fireball 的 define() 方法中的body = world.createBody(bodyDef)这一行崩溃了。

com.badlogic.gdx.physics.box2d.World.jniCreateBody

You are trying to access memory that is no longer available.您正在尝试访问不再可用的内存。 There are several tutorials about how to create and destroy bodies in box2d.有几个关于如何在 box2d 中创建和销毁实体的教程。 You have to do it at the right time with regards to updating the physics world (step()).你必须在正确的时间更新物理世界(step())。

Since you only get JNI debug info it is quite hard to know exactly where the bug is (been there many times) A good way to get a better exception is to set the body to null (right after world.destroyBody(body);)由于您只获得 JNI 调试信息,因此很难确切知道错误在哪里(多次出现)获得更好异常的好方法是将主体设置为 null(紧跟在 world.destroyBody(body); 之后)

world.destroyBody(body);
body = null;
destroyed = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复java.lang.UnsatisfiedLinkError:com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()? - How to fix java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()? Libgdx Box2d Java崩溃-jniCreateBody - Libgdx Box2d Java Crash - jniCreateBody NoClassDefFoundError:com / badlogic / gdx / LifecycleListener - NoClassDefFoundError: com/badlogic/gdx/LifecycleListener Libgdx scene2d uiskin.json com.badlogic.gdx.utils.GdxRuntimeException - Libgdx scene2d uiskin.json com.badlogic.gdx.utils.GdxRuntimeException libgdx无法拖动com.badlogic.gdx.scenes.scene2d.ui.Window - libgdx can't drag com.badlogic.gdx.scenes.scene2d.ui.Window 为什么“没有com.badlogic.gdx.scenes.scene2d.ui.ScrollPane $ ScrollPaneStyle注册名称:默认”错误即将到来以及如何解决? - Why “No com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle registered with name: default” error is coming and how to fix it? 构造函数的com.badlogic.gdx.utils.Array参数 - com.badlogic.gdx.utils.Array parameters for Constructor com.badlogic.gdx.ApplicationListener类型无法解析 - The type com.badlogic.gdx.ApplicationListener cannot be resolved 导入com.badlogic.gdx.graphics.gl10无法解析 - the import com.badlogic.gdx.graphics.gl10 cannot be resolved 在Main类中使用libgdx com.badlogic.gdx.Preferences? - Using a libgdx com.badlogic.gdx.Preferences in the Main class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM