Java 尝试更改我的参与者在渲染中的位置时,libGDX中出现空指针异常
当我试图运行这段代码时,它只在我调用actor的方法“isPulsado”或“setPosition”时,才会给我的应用程序一个Null指针异常 渲染方法:Java 尝试更改我的参与者在渲染中的位置时,libGDX中出现空指针异常,java,nullpointerexception,libgdx,global-variables,Java,Nullpointerexception,Libgdx,Global Variables,当我试图运行这段代码时,它只在我调用actor的方法“isPulsado”或“setPosition”时,才会给我的应用程序一个Null指针异常 渲染方法: public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); lienzo.setProjectionMatrix(cam.combined); shaper1.setProjectionMatrix(cam.combine
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
lienzo.setProjectionMatrix(cam.combined);
shaper1.setProjectionMatrix(cam.combined);
shaper2.setProjectionMatrix(cam.combined);
lienzo.begin();
shaper1.begin(ShapeRenderer.ShapeType.Filled);
shaper1.setColor(1, 1, 1, 1);
shaper2.begin(ShapeRenderer.ShapeType.Filled);
shaper2.setColor(0.545f, 0.271f, 0.075f, 1f);
for (int i = 0; i <= 300; i += 100) {
for (float j = 0; j <= 300; j += 100) {
shaper1.box(i, j, 10, 50, 50, 10);
shaper1.box(i + 50, j + 50, 10, 50, 50, 10);
}
for (int j = 0; j <= 300; j += 100) {
shaper2.box(i, j + 50, 10, 50, 50, 10);
shaper2.box(i + 50, j, 10, 50, 50, 10);
}
}
shaper1.end();
shaper2.end();
lienzo.end();
stage.act(Gdx.graphics.getDeltaTime());
if(reyBlanco.isPulsado())reyBlanco.setPosition(Gdx.input.getX() - 20, -Gdx.input.getY() + 375);
stage.draw();
}
我在create方法中初始化了actor,并将其放置在表中
public void create() {
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
Rey reyBlanco = new Rey(150, 0);
Reina reinaBlanco = new Reina(200, 0);
stage.addActor(reyBlanco);
stage.addActor(reinaBlanco);
cam = new OrthographicCamera(ancho, alto);
lienzo = new SpriteBatch();
shaper1 = new ShapeRenderer();
shaper2 = new ShapeRenderer();
}
NPE-因为您甚至没有初始化全局变量
reyBlanco
应该是
reyBlanco = new Rey(150, 0);
而不是
Rey reyBlanco = new Rey(150, 0); // This is local initialization in your code.
上行将在当前范围内创建一个名为
reyBlanco
的新变量,并覆盖全局变量。全局设置仍然是null
您是否初始化了reyBlanco
?是的,我更新了问题
Rey reyBlanco = new Rey(150, 0); // This is local initialization in your code.