尝试我的游戏,当我从Java运行时环境EXCEPTION_ACCESS_VIOLATION崩溃。它与LibGdx书面(并使用Box2D的)。它是从机器人工作室的桌面模式下运行。
我加了一个“火球”功能,我supermario游戏,并跳进空气和拍摄火球时出现此错误。这里是崩溃日志:
的Java帧:(J =编译的Java代码,J =解释,VV = VM代码).J com.badlogic.gdx.physics.box2d.World.jniCreateBody(JIFFFFFFFFZZZZZF)J +为0J com.badlogic.gdx.physics.box2d。 World.createBody(LCOM / badlogic / GDX /物理/ Box2D的/ BodyDef)LCOM / badlogic / GDX /物理/ Box2D的/身体; 80Ĵcom.mygdx.game.sprite.Fireball.define()V + 68ĴCOM .mygdx.game.sprite.Fireball(LCOM / mygdx /游戏/屏幕/播放屏幕; FFZ)。V + 135Ĵ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] Ĵ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Ĵcom.badlogic.gdx.backends.lwjgl.LwjglApplic通货膨胀$ 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;
}
}
我叫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);
}
为什么我得到这个错误任何想法?
编辑:现在看来似乎崩溃在这条线body = world.createBody(bodyDef)
中定义火球()方法。
com.badlogic.gdx.physics.box2d.World.jniCreateBody
您试图访问内存不再可用。对于如何创建和销毁Box2D的几个机构教程。您有关于做在正确的时间,以更新物理世界(步骤())。
既然你只能得到JNI调试信息是非常难以知道确切位置是(去过那里很多次)的错误的一个好方法,以获得更好的例外是对身体设置为null(右后world.destroyBody(身体);)
world.destroyBody(body);
body = null;
destroyed = true;