문제

I created a super class with nothing in the render method:

public class SuperClass implements InputProcessor {

    public SuperClass(MyGame game) {

    }

    public void render() {

    }

    // InputProcessor overriden methods here
}

In a subclass I override the render method:

public class SubClass extends SuperClass {

Stage stage;

public SubClass(MyGame game) {
    super(game);

    stage = new Stage(0, 0, false);

    skin = new Skin(Gdx.files.internal("data/uiskin.json"));

    Gdx.input.setInputProcessor(stage);

    Image image = new Image(new Texture(Gdx.files.internal("bg.png")));
    image.setPosition(0, 0);
    image.setWidth(800);
    image.setHeight(480);
    stage.addActor(image);

}

@Override
public void render() {

    stage.act(Gdx.graphics.getDeltaTime());

    stage.draw();

}

}

But the stage doesn't get drawn. I know this because I expect to see the background image but its just a blank screen.

What am I doing wrong here?

도움이 되었습니까?

해결책 3

The line in the SubClass constructor should be:

stage = new Stage(800, 480, false);

or you can also do this:

stage = new Stage()

but then you have to set the viewport, stage.setViewport(float, float, boolean) in resize(float, float).

다른 팁

You should be calling your SuperClass render method in your MyGame render method.

Is your render() method being called each game loop? What I do normally is to create an Object which extends Game and set a Screen.

class MyGame extends Game {

  @Override
  public void create() {

    setScreen(new MainScreen());
  }       
}

In the MainScreen class which implements Screen interface I can create my stage:

class MainScreen implements Screen {

    Stage stage = new Stage(0, 0);


    @Override
    public void dispose() {
        stage.dispose();

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void render(float delta) {


        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();


    }

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

    }

    @Override
    public void resume() {
    }

    @Override
    public void show() {
    stage = new Stage(0, 0, false);

         skin = new Skin(Gdx.files.internal("data/uiskin.json"));

         Gdx.input.setInputProcessor(stage);

         Image image = new Image(new Texture(Gdx.files.internal("bg.png")));
         image.setPosition(0, 0);
         image.setWidth(800);
         image.setHeight(480);
         stage.addActor(image);

    }

}

With this code the render() method should be called in each game loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top