gpt4 book ai didi

java - 空指针异常?不知道为什么。 LIBGDX

转载 作者:行者123 更新时间:2023-11-29 21:41:43 27 4
gpt4 key购买 nike

我一直在第 39 行收到 NullPointerException,这是modelBatch.begin(cam);

我不知道为什么要这样做。如果你注意到原因,请告诉我。我整天都在为这个问题而苦苦挣扎。我是 android 开发的新手,很容易犯愚蠢的错误。感谢您的帮助。

public class Loading implements Screen {

private boolean AP;
private Chemistry chemistry;
public PerspectiveCamera cam;
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Lights lights;

public Loading(boolean AP, Chemistry chemistry) {
this.AP = AP;
this.chemistry = chemistry;
}

@Override
public void render(float delta) {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

modelBatch.begin(cam);
modelBatch.render(instance, lights);
modelBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void show() {
modelBatch = new ModelBatch();
lights = new Lights();
lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
-0.2f));

cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();

ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);

instance = new ModelInstance(model);

}
...
}

最佳答案

将其移至您的构造函数:

        modelBatch = new ModelBatch();
lights = new Lights();
lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
-0.2f));

cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();

ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);

instance = new ModelInstance(model);

喜欢:

public Loading(boolean AP, Chemistry chemistry) {
this.AP = AP;
this.chemistry = chemistry;
//here
}

关于java - 空指针异常?不知道为什么。 LIBGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16888085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com