尝试游戏时,我在Java运行时环境中遇到EXCEPTION_ACCESS_VIOLATION崩溃。它是用LibGdx编写的(并使用box2d)。它在Android Studio的桌面模式下运行。
我在我的supermario游戏中添加了“火球”功能,当跳入空中射击火球时出现此错误。这是崩溃日志:
Java框架:(J =编译的Java代码,j =解释的,Vv = VM代码)j com.badlogic.gdx.physics.box2d.World.jniCreateBody(JIFFFFFFFFZZZZZF)J + 0 Ĵ 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.mygdx.game.MarioBros.render()V(5字节)@ 0x0000000003263ae4 [0x0000000003263920 + 0x1c4] j com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V + 698 j com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run()V + 27 v 〜StubRoutines :: call_stub
这是我的火球课:
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;
}
}
我从PlayScreen类打来mario.shootFire()
。
这是“马里奥射击火球”的方式:
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);
}
关于我为什么会收到此错误的任何想法?
编辑:看来它在Fireball的define()方法的body = world.createBody(bodyDef)
行崩溃了。
答案 0 :(得分:1)
com.badlogic.gdx.physics.box2d.World.jniCreateBody
您正在尝试访问不再可用的内存。有一些关于如何在box2d中创建和销毁实体的教程。关于更新物理世界,您必须在正确的时间进行(step())。
由于您仅获得JNI调试信息,因此很难确切地知道错误的位置(存在多次),获得更好的异常的一种好方法是将body设置为null(在world.destroyBody(body之后);)
world.destroyBody(body);
body = null;
destroyed = true;