我点击了我能找到的每一个Google结果,这可能对我没有成功有所帮助。
我已经构建了一个游戏,我希望用它来实现完整的UI。在我的游戏中,我为我的精灵设置了单独的类,所以在做几个制作屏幕的教程时,我试图实现一些我可以使用这些单独的类来获取纹理,当前帧等的东西。
我已经到了可以在两个屏幕之间切换的地步,如果它们不是指外面的任何东西,但每当我尝试在游戏屏幕上引用我的精灵类时,它会崩溃,在游戏中指向我类:
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
所以这是我正在使用的代码(减去任何实现的方法或其他默认类的东西)。
Tutorial.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"));
}
}
Player.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,它就可以工作。这虽然不适用于播放器......
答案 0 :(得分:3)
AnotherScreen的工作原理是因为您在使用之前已经实例化了批处理。但是,在MainMenuScreen中,您没有实例化批处理和播放器对象,ergo,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();
}
}
答案 1 :(得分:1)
在 MainMenuScreen.java 中,您尚未实例化SpriteBatch
。只是做:
batch = new SpriteBatch();
在使用之前,在show()
或构造函数中。
编辑:
我也没有看到你的player
在任何地方被实例化。