1
我對libgdx很陌生,目前正在嘗試創建遊戲,但在嘗試創建播放器時遇到線程「LWJGL Application」java.lang.NullPointerException錯誤中的異常 this是我的代碼:無法在libgdx中定義播放器
public class OfficeMayhem extends Game {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final float PPM = 100;
public static final short GROUND_BIT = 1;
public static final short PLAYER_BIT = 2;
public static final short ENEMY_BIT = 4;
public static final short POWER_BIT = 8;
public SpriteBatch batch;
@Override
public void create() {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}
@Override
public void render() {
super.render();
}
@Override
public void dispose(){
super.dispose();
batch.dispose();
}
}
public class PlayScreen implements Screen {
private OfficeMayhem game;
private TextureAtlas atlas;
private OrthographicCamera gamecam;
private Viewport gamePort;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private B2WorldCreator creator;
private Player player;
public BodyDef bdef;
public static SpriteBatch batch = new SpriteBatch();
public PlayScreen(OfficeMayhem game)
{
atlas = new TextureAtlas("Game_Pack.pack");
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(OfficeMayhem.WIDTH , OfficeMayhem.HEIGHT, gamecam);
maploader = new TmxMapLoader();
map = maploader.load("level.tmx");
renderer = new OrthogonalTiledMapRenderer(map,1);
gamecam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2,0);
player = new Player(this);
world = new World(new Vector2(0,-10),true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth()/2), (rect.getY() + rect.getHeight()/2));
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth()/2, rect.getHeight()/2);
fdef.shape = shape;
body.createFixture(fdef);
}
}
public TextureAtlas getAtlas(){
return atlas;
}
public void handleInput(float dt){
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
}
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
//gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
renderer.setView(gamecam);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gamecam.combined);
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
}
public World getWorld(){return world;}
public TiledMap getMap(){
return map;
}
}
public class Player extends Sprite {
public World world;
public Body b2body;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Texture texture;
private TextureRegion playerStand;
public Player (PlayScreen screen){
this.world = screen.getWorld();
playerStand = new TextureRegion(screen.getAtlas().findRegion("book"),0, 0, 60, 60);
definePlayer();
setBounds(0,0, 60, 60);
setRegion(playerStand);
}
public void update(float dt){
setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2 - 6);
setRegion(playerStand);
}
public void definePlayer(){
BodyDef bdef = new BodyDef();
bdef.position.set(32 , 32);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(6);
fdef.filter.categoryBits = OfficeMayhem.PLAYER_BIT;
fdef.filter.maskBits =
OfficeMayhem.GROUND_BIT |
OfficeMayhem.ENEMY_BIT;
fdef.shape = shape;
b2body.createFixture(fdef).setUserData(this);
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData(this);
}
}
public class B2WorldCreator {
public B2WorldCreator(PlayScreen screen) {
World world = screen.getWorld();
TiledMap map = screen.getMap();
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth()/2), (rect.getY() + rect.getHeight()/2));
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth()/2, rect.getHeight()/2);
fdef.shape = shape;
body.createFixture(fdef);
}
}
}
,我得到這個錯誤:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.podariudragos.officemayhem.Player.definePlayer(Player.java:52)
at com.podariudragos.officemayhem.Player.<init>(Player.java:37)
at com.podariudragos.officemayhem.PlayScreen.<init>(PlayScreen.java:68)
at com.podariudragos.officemayhem.OfficeMayhem.create(OfficeMayhem.java:25)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
誰能幫我,我不能找出我做錯了嗎?
問題是當我嘗試在播放器類中創建主體時,該行具體爲:b2body = world.createBody(bdef); –
可能重複的[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -我修復它) – Tenfour04