播放屏幕類:LIBGDX:調用另一個類中的矩形導致NullPointerException?
public class PlayScreen implements Screen {
private Player player;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private TiledMap map;
private Rectangle rightRectangle, leftRectangle, playerRectangle;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());
renderer.getSpriteBatch().end();
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void show() {
camera = new OrthographicCamera();
map = new TiledMap();
renderer = new OrthogonalTiledMapRenderer(map);
player = new Player(new Sprite(new Texture("img/player.png")), playerRectangle);
playerRectangle = player.rectangle;
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
boolean wallLeft = leftRectangle.overlaps(player.rectangle);
boolean wallRight = rightRectangle.overlaps(player.rectangle);
if(wallLeft){
System.out.println("wallLeft Overlap");
player.velocity.x = 0;
}
else if(wallRight){
System.out.println("wallRight Overlap");
player.velocity.x = 0;
}
player.setPosition(Gdx.graphics.getWidth()/2f - player.getWidth()/2f,Gdx.graphics.getHeight()/2f - player.getHeight()/2f - Gdx.graphics.getHeight()/5f);
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
map.dispose();
player.getTexture().dispose();
renderer.dispose();
}
}
Player類:
public class Player extends Sprite{
public Vector2 velocity = new Vector2();
private float speed = 500;
public Rectangle rectangle;
public Player(Sprite sprite, Rectangle rectangle){
super(sprite);
this.rectangle = rectangle;
}
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public void update(float delta) {
rectangle = new Rectangle(getX() + velocity.x * delta,0,286,160);
setX(getX() + velocity.x * delta);
}
}
所以,我用矩形來檢測我的衝突。我已經設置rightRectangle和leftRectangle作爲我的屏幕邊界。我想給玩家分配一個精靈和一個矩形。我正在處理PlayScreen類中的碰撞,但它導致了NullPointerException。需要幫助確定爲什麼我得到NullPointerException。
謝謝!
NullPointerException異常:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.math.Rectangle.overlaps(Rectangle.java:210)
at net.dermetfan.asteroids.screens.PlayScreen.show(PlayScreen.java:62)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at net.dermetfan.asteroids.screens.MainMenu$1.clicked(MainMenu.java:75)
at
com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:82)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:329)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:209)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
安置自己的堆棧跟蹤,所以我們可以看到空指針異常。 –