我点击了我能找到的所有可能对我有帮助的 Google 结果,但没有成功。
我构建了一个游戏,我想用它来实现完整的 UI。在我的游戏中,我为 Sprite 设置了单独的类,因此在制作屏幕的几个教程中,我尝试实现一些可以使用这些单独的类来获取纹理、当前帧等的东西。
我已经到了可以在两个屏幕之间切换的地步,如果它们不引用任何外部的东西,但每当我尝试在游戏屏幕上引用我的 sprite 类时,它就会崩溃,并在游戏中指向这个类:
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
所以这是我正在使用的代码(减去任何已实现的方法或类默认的其他内容)。
教程.java:
public class Tutorial extends Game {
MainMenuScreen mainMenuScreen ;
AnotherScreen anotherScreen;
Player player ;
@Override
public void create() {
mainMenuScreen = new MainMenuScreen(this);
anotherScreen = new AnotherScreen(this);
setScreen(mainMenuScreen);
}
}
MainMenuScreen.java(问题出在批处理中):
public class MainMenuScreen implements Screen {
Tutorial game ;
SpriteBatch batch ;
Player player ;
Texture test ;
public MainMenuScreen(Tutorial game){
this.game = game;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(player.playerTexture, 200, 200); // error points to here
batch.end();
}
}
AnotherScreen.java(运行良好):
public class AnotherScreen implements Screen {
Tutorial game ;
SpriteBatch batch ;
Texture test ;
public AnotherScreen(Tutorial game){
this.game = game;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(test, 0, 200);
batch.end();
}
@Override
public void show() {
batch = new SpriteBatch();
test = new Texture(Gdx.files.internal("badlogic.jpg"));
}
}
播放器.java:
public class Player {
Texture playerTexture ;
Vector2 position;
String textureLoc;
public Player(Vector2 position, String textureLoc){
//What I am trying to get from AnotherScreen.java
playerTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
}
public Texture getPlayerTexture() {
return playerTexture;
}
public void setPlayerTexture(Texture playerTexture) {
this.playerTexture = playerTexture;
}
}
控制台中的确切错误是:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.tutorial.MainMenuScreen.render(MainMenuScreen.java:25)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
我做错了什么?我重新安排了将所有内容放在不同位置的代码,但我就是无法让它工作。感谢您的宝贵时间!
我发现,如果我打开 MainMenuScreen,然后转到 AnotherScreen 并尝试从 Main 获取纹理以显示在 Another 中,如果我让 Tutorial 在将纹理传递给 Another 之前获取纹理,它会起作用。虽然这不适用于 Player ...
最佳答案
AnotherScreen 之所以有效,是因为您在使用它之前已经实例化了批处理。但是,在 MainMenuScreen 中,您没有同时实例化批处理对象和播放器对象,因此,NPE。确保您已经实例化了批处理和播放器纹理,如下所示:
public class MainMenuScreen implements Screen {
Tutorial game ;
SpriteBatch batch ;
Player player ;
Texture test ;
public MainMenuScreen(Tutorial game){
this.game = game;
batch = new SpriteBatch();
player = new Player(position, textureLoc); //modify arguments
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(player.playerTexture, 200, 200); // error points to here
batch.end();
}
}
关于android - 引用另一个类时出现 Libgdx Screen NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23962321/