0
我正在使用Box2D跳轉並運行遊戲,並希望玩家在碰到尖峯時被傳送到開始位置,但當他遇到尖峯時,我會得到空指針異常。Libgdx null指針
繼承人我跟類:
public class WorldContactListener implements ContactListener {
Player player;
@Override
public void beginContact(Contact contact) {
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
player = new Player();
int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits;
switch (cDef) {
case HardwareRunner.PLAYER_BIT | HardwareRunner.BRICK_BIT:
case HardwareRunner.PLAYER_BIT | HardwareRunner.SPIKE_BIT:
player.die();
}
}
而且我的繼承人播放器類的部分:
public void definePlayer(){
bdef.position.set(32/runner.PPM, (6 * 32)/runner.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
Vector2[] vertice = new Vector2[4];
vertice[0] = new Vector2(-13, 13).scl(1/runner.PPM);
vertice[1] = new Vector2(13, 13).scl(1/runner.PPM);
vertice[2] = new Vector2(13, -13).scl(1/runner.PPM);
vertice[3] = new Vector2(-13, -13).scl(1/runner.PPM);
shape.set(vertice);
shape.getRadius();
fdef.filter.categoryBits = HardwareRunner.PLAYER_BIT;
fdef.filter.maskBits =
HardwareRunner.GROUND_BIT |
HardwareRunner.COIN_BIT |
HardwareRunner.BRICK_BIT |
HardwareRunner.ENEMY_BIT |
HardwareRunner.SPIKE_BIT |
HardwareRunner.ENEMY_HEAD_BIT |
HardwareRunner.ITEM_BIT;
fdef.shape = shape;
fdef.friction = .1f;
b2body.createFixture(fdef).setUserData(this);
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData(this);
}
public void die() {
world.destroyBody(b2body);
bdef.position.set(32/runner.PPM, (6 * 32)/runner.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
Vector2[] vertice = new Vector2[4];
vertice[0] = new Vector2(-13, 13).scl(1/runner.PPM);
vertice[1] = new Vector2(13, 13).scl(1/runner.PPM);
vertice[2] = new Vector2(13, -13).scl(1/runner.PPM);
vertice[3] = new Vector2(-13, -13).scl(1/runner.PPM);
shape.set(vertice);
shape.getRadius();
fdef.filter.categoryBits = HardwareRunner.PLAYER_BIT;
fdef.filter.maskBits =
HardwareRunner.GROUND_BIT |
HardwareRunner.COIN_BIT |
HardwareRunner.BRICK_BIT |
HardwareRunner.ENEMY_BIT |
HardwareRunner.SPIKE_BIT |
HardwareRunner.ENEMY_HEAD_BIT |
HardwareRunner.ITEM_BIT;
fdef.shape = shape;
fdef.friction = .1f;
b2body.createFixture(fdef).setUserData(this);
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData(this);
}
和錯誤是:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at de.tobls.hardwarerunner.Sprites.Player.die(Player.java:223)
at de.tobls.hardwarerunner.Tools.WorldContactListener.beginContact(WorldContactListener.java:27)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:982)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
at com.badlogic.gdx.physics.box2d.World.step(World.java:686)
at de.tobls.hardwarerunner.Screens.PlayScreen.update(PlayScreen.java:112)
at de.tobls.hardwarerunner.Screens.PlayScreen.render(PlayScreen.java:127)
at com.badlogic.gdx.Game.render(Game.java:46)
at de.tobls.hardwarerunner.HardwareRunner.render(HardwareRunner.java:71)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
誰能幫助我?
我做到了,但我仍然得到這個錯誤。我能對付這個錯誤做什麼? – Tobls
請指出錯誤所在的行 - 我在棧跟蹤中看到但沒有代碼 –
錯誤在行中:world.destroyBody(b2body);我認爲問題出在b2body上,因爲無論我在die()中寫什麼與b2body有什麼關係,我都會得到該錯誤 – Tobls