我开始尝试创建一个TestGame。目前有一个NPE,并在谷歌上花了几个小时,并尝试我能想到的一切,我似乎无法理解我正在忽视或遗忘。我把所有东西放在一个班级里,一切都很完美。虽然我想创建一个GUI,我可以在MainMenu和PlayScreen之间切换。我的MainMenu类工作正常,但是当我按下我整洁的小“播放”按钮进入PlayScreen类时,我得到了这些错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.me.game.PlayScreen.render(PlayScreen.java:90)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.me.game.TestGame.render(TestGame.java:27)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
播放屏错误:
batch.draw(player.getCurrentFrame(), player.getPosition().x , player.getPosition().y);
游戏错误:
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
TestGame错误:
super.render();
TestMenu类(游戏类)
public class TestGame extends Game {
Game game;
@Override
public void create() {
game = this;
setScreen(new MainMenu(game));
public void render() {
super.render();
}
MainMenu 类
public class MainMenu implements Screen{
SpriteBatch batch;
Game game;
Stage stage;
public MainMenu(Game game){
this.game = game;
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
batch.begin();
stage.draw();
batch.end();
}
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new PlayScreen(game));
stage.clear();
return true;
播放屏幕
public class PlayScreen implements Screen {
SpriteBatch batch;
Player player;
Game game;
Texture playerTexture;
public PlayScreen(Game game){
this.game = game;
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(player.getCurrentFrame(), player.getPosition().x , player.getPosition().y);
}
player.update();
batch.end();
}
public void show() {
batch = new SpriteBatch();
玩家类
public class Player {
Texture playerTexture ;
Vector2 position;
String textureLoc;
public Player(Vector2 position, String textureLoc){
this.position = position;
movement = "";
playerTexture = new Texture(Gdx.files.internal("Character.png"));
public String getTextureLoc() {
return textureLoc;
}
public void setTextureLoc(String textureLoc) {
this.textureLoc = textureLoc;
}
最有帮助的是Libgdx Screen NullPointerException when referencing another class,其中问题似乎也是我没有在show方法中初始化PlayerScreen类中的播放器。虽然当我填充player = new Player(position,textureLoc)时;在show方法下
public void show() {
batch = new SpriteBatch();
player = new Player(position, textureLoc);
我收到其他错误
at com.me.game.Player.<init>(Player.java:50)
at com.me.game.PlayScreen.show(PlayScreen.java:90)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.me.game.MainMenu$1.touchDown(MainMenu.java:90)
指向:
bounds = new Rectangle(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
player = new Player(position, textureLoc);
game.setScreen(new PlayScreen(game));
虽然我认为它与未正确初始化的播放器有关?无论如何由于某种原因我似乎无法解决这个问题。任何想法都将不胜感激。
答案 0 :(得分:0)
问题就在这里
player = new Player(position, textureLoc);
position和textureLoc是占位符,您创建的这些参数如下
Vector2 position, String textureLoc
这意味着您需要创建一个新的Vector2并将其放置在position和String之间并将其放置在textureLoc所在的位置。
请尝试使用以下代码,这样您的播放器就会大致位于屏幕中间。
player = new Player(new Vector2(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2), "I am not sure why you put this parameter here");