我实际上遇到了 libgdx 的问题。
打字游戏.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 更改当前屏幕。这是我所做的:
游戏画面.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。
非常感谢您的帮助,感谢并为我的英语不好感到抱歉。
啊!