舞台将不会在 libgdx 中呈现,出现空指针错误
Stage will not render in libgdx, get null pointer error
我只是想让 Actor 和 Stage 正常工作以设置基本流程,然后从那里继续。我每次都会得到一个指向舞台的空指针,请帮忙。 Paddle 和 Ball class 现在完全相同,Assets 是用于加载纹理的静态 class。
public class MyGame implements ApplicationListener {
public final static int WIDTH = 480;
public final static int HEIGHT = 800;
private Stage stage;
private Paddle paddle;
private Ball ball;
@Override
public void create () {
Assets.load();
Stage stage = new Stage(new ScreenViewport());
paddle = new Paddle();
ball = new Ball();
stage.addActor(paddle);
stage.addActor(ball);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void resize(int width, int height){
stage.getViewport().update(width,height,true);
}
@Override
public void dispose(){
Assets.dispose();
stage.dispose();
}
public int getWidth(){return WIDTH;}
public int getHeight(){return HEIGHT;}
}
public class Paddle extends Actor {
Rectangle bounds;
public Paddle(){
setPosition(150,10);
}
@Override
public void act(float delta){
}
public void draw(Batch batch , float parentAlpha){
batch.draw(Assets.paddle,150,10 );
}
private void updateBounds() {bounds.set(getX(), getY(), getWidth(), getHeight());
}
public Rectangle getBounds() {
return bounds;
}
}
问题是,您在 create 方法中创建了一个 Stage
,但您从未将其分配给您的私有成员 stage
。
所以不要在 create
中写 Stage stage = new Stage()
只需写 stage = new Stage()
.
还要记住将 Exception
和它的 Stack Trace
添加到您的 SO-Question 中,并标记 Exception
似乎出现的行。这将使每个想要提供帮助的人都更容易。
删除同一变量的第二个阶段声明"stage"
@Override
public void create () {
Assets.load();
stage = new Stage(new ScreenViewport());
paddle = new Paddle();
ball = new Ball();
stage.addActor(paddle);
stage.addActor(ball);
}
我只是想让 Actor 和 Stage 正常工作以设置基本流程,然后从那里继续。我每次都会得到一个指向舞台的空指针,请帮忙。 Paddle 和 Ball class 现在完全相同,Assets 是用于加载纹理的静态 class。
public class MyGame implements ApplicationListener {
public final static int WIDTH = 480;
public final static int HEIGHT = 800;
private Stage stage;
private Paddle paddle;
private Ball ball;
@Override
public void create () {
Assets.load();
Stage stage = new Stage(new ScreenViewport());
paddle = new Paddle();
ball = new Ball();
stage.addActor(paddle);
stage.addActor(ball);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void resize(int width, int height){
stage.getViewport().update(width,height,true);
}
@Override
public void dispose(){
Assets.dispose();
stage.dispose();
}
public int getWidth(){return WIDTH;}
public int getHeight(){return HEIGHT;}
}
public class Paddle extends Actor {
Rectangle bounds;
public Paddle(){
setPosition(150,10);
}
@Override
public void act(float delta){
}
public void draw(Batch batch , float parentAlpha){
batch.draw(Assets.paddle,150,10 );
}
private void updateBounds() {bounds.set(getX(), getY(), getWidth(), getHeight());
}
public Rectangle getBounds() {
return bounds;
}
}
问题是,您在 create 方法中创建了一个 Stage
,但您从未将其分配给您的私有成员 stage
。
所以不要在 create
中写 Stage stage = new Stage()
只需写 stage = new Stage()
.
还要记住将 Exception
和它的 Stack Trace
添加到您的 SO-Question 中,并标记 Exception
似乎出现的行。这将使每个想要提供帮助的人都更容易。
删除同一变量的第二个阶段声明"stage"
@Override
public void create () {
Assets.load();
stage = new Stage(new ScreenViewport());
paddle = new Paddle();
ball = new Ball();
stage.addActor(paddle);
stage.addActor(ball);
}