我實際上遇到了libgdx的問題。LIBGDX - SetScreen空指針異常
TypingGame.java
public class TypingGame extends Game {
TypingGame game;
GameScreen gameScreen;
GameOverScreen gameOverScreen;
@Override
public void create() {
gameScreen = new GameScreen();
gameOverScreen = new GameOverScreen(game);
Texture.setEnforcePotImages(false);
setScreen(new GameScreen()); // Default screen : Gamescreen
}
}
我希望能夠通過使用setScreen改變當前屏幕。下面是我所做的:
GameScreen.java
public class GameScreen implements Screen, InputProcessor{
private World world;
private WorldRenderer renderer;
private WorldController controller;
TypingGame game;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Timer.elapsedTime(delta);
renderer.render(delta);
controller.update(delta);
if(Player.health == 0) game.setScreen(new GameOverScreen(game)); // If the player died, switch to GameOverScreen
}
// And so on...
GameOverScreen.java
public class GameOverScreen implements Screen{
TypingGame game;
GameScreen gameScreen;
public GameOverScreen(TypingGame game){
this.game = game;
gameScreen = new GameScreen();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
我想不通,爲什麼我有這樣的錯誤:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NullPointerException
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.NullPointerException
這應該是相當的易於使用,但唯一能實際使用setScreen的類是... TypingGame.java。
您的幫助將非常感謝,感謝和抱歉我的英語不好。
Cya!
完美的我知道了,非常感謝。 – user2649458