我正在尝试在游戏调整大小时更新游戏的视口,但是当我开始游戏时,我得到java.lang.NullPointerException
:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.utils.viewport.Viewport.apply(Viewport.java:49)
at com.badlogic.gdx.utils.viewport.ExtendViewport.update(ExtendViewport.java:90)
at com.badlogic.gdx.utils.viewport.Viewport.update(Viewport.java:57)
at me.chrisjosten.testgame.screens.MainScreen.resize(MainScreen.java:82)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at me.chrisjosten.testgame.create(MainScreen.java:13)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
在实现Screen
的类中,我在resize方法中发生了异常的地方,代码如下:
@Override
public void resize(int w, int h) {
viewport.update(w, h);
}
其中视口是在类的构造函数中创建的ExtendViewport。我已经尝试将它放在show方法中,但后来我得到了相同的结果。
班级的完整代码:
package me.chrisjosten.testgame.screens;
import me.chrisjosten.testgame.MyGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MainScreen implements Screen{
private MyGame game;
private BitmapFont font;
private SpriteBatch batch;
private OrthographicCamera camera;
private Viewport viewport;
private boolean goingUp = false;
private float alpha = 1;
private int gameWidth = 100;
private int gameHeight = 100;
private int screenWidth;
private int screenHeight;
public MainScreen(MyGame g) {
game = g;
System.out.println("screen created");
}
@Override
public void show() {
System.out.println("show");
font = new BitmapFont(Gdx.files.internal("fonts/monospace.fnt"));
font.setColor(1, 1, 1, 1);
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.translate(gameWidth / 2, gameHeight / 2);
viewport = new ExtendViewport(gameWidth, gameHeight, camera);
}
@Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (goingUp) {
font.setColor(1, 1, 1, alpha);
alpha += 0.025;
} else {
font.setColor(1, 1, 1, alpha);
alpha -= 0.025;
}
if (alpha <= 0) {
goingUp = true;
alpha = 0;
} else if (alpha >= 1) {
goingUp = false;
alpha = 1;
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
String press = "Press start";
font.draw(batch, press, gameWidth / 2 - font.getBounds(press).width / 2, 1);
batch.end();
}
@Override
public void resize(int w, int h) {
viewport.update(w, h);
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
font.dispose();
}
}
有谁知道我做错了什么?
答案 0 :(得分:0)
通过将代码从show
方法移到构造函数,它可以工作。