2
\$\begingroup\$

I've been having trouble with getting scene2d to erase my old texture when I switch screens, and also when I change my label the old label string is still drawn underneath.

public StartScreen(MainGame game){
    this.mainGame = game;

    viewport = new FitViewport(640, 360, new OrthographicCamera());
    stage = new Stage(viewport, game.batch);

    Texture button = new Texture("playButton.png");
    Button.ButtonStyle bs = new Button.ButtonStyle();
    bs.up = new TextureRegionDrawable(new TextureRegion(button));
    startGameButton = new Button(bs);

    Table table = new Table();
    table.top();
    table.setFillParent(true);

    table.add(startGameButton).expandX().padTop(10);

    stage.addActor(table);

    startGameButton.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            mainGame.SetClassScreen();
        }
    });

    Gdx.input.setInputProcessor(stage);
}

public void render(float delta) {
    mainGame.batch.setProjectionMatrix(stage.getCamera().combined);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

In the constructor I have tried using Dispose() when changing the screen and it doesn't work.

When the screens are changed, The playButton still shows in the next screen. I also have a Label which displays text depending on a button pressed, but when it's pressed it writes the string over the old string to look like a jumbled mess.

I have a feeling I'm not updating something to be redrawn somewhere but I can't find it.

public PickClassScreen(MainGame game){
    this.mainGame = game;

    viewport = new FitViewport(640, 360, new OrthographicCamera());
    stage = new Stage(viewport, game.batch);

    //All initialization of Scene2d stuff.

    stage.addActor(table);

    Gdx.input.setInputProcessor(stage);
}

@Override
public void show() {

}

@Override
public void render(float delta) {
    mainGame.batch.setProjectionMatrix(stage.getCamera().combined);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

}

The screen that I am switching to. ^

\$\endgroup\$
6
  • \$\begingroup\$ What does the SetClassScreen method do? \$\endgroup\$
    – bornander
    Commented Nov 14, 2016 at 9:11
  • \$\begingroup\$ in the MainGame.java class its just setScreen(new PlayScreen(this)); \$\endgroup\$ Commented Nov 14, 2016 at 9:13
  • \$\begingroup\$ So what does the constructor for PlayScreen look like? Are you setting up a Stage in that as well? \$\endgroup\$
    – bornander
    Commented Nov 14, 2016 at 9:24
  • \$\begingroup\$ it should actually be a screen called PickClassScreen but yes I'm setting up a stage in that as well. For each of my three screens I have a new stage \$\endgroup\$ Commented Nov 14, 2016 at 9:27
  • \$\begingroup\$ So what does the constructor look like? Without seeing more of your code it's difficult to help. \$\endgroup\$
    – bornander
    Commented Nov 14, 2016 at 9:42

1 Answer 1

1
\$\begingroup\$

Try changing your render methods to this;

For StartScreen:

public void render(float delta) {
  // Clear the buffer
  Gdx.gl.glClearColor(0, 0, 0, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

  mainGame.batch.setProjectionMatrix(stage.getCamera().combined);
  stage.act(Gdx.graphics.getDeltaTime());
  stage.draw();
}

For PickClassScreen

public void render(float delta) {
  // Clear the buffer
  Gdx.gl.glClearColor(0, 0, 0, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

  mainGame.batch.setProjectionMatrix(stage.getCamera().combined);
  stage.act(Gdx.graphics.getDeltaTime());
  stage.draw();
}

I think you're seeing old stuff on the render buffer, by clearing the buffer before every draw you wont have this issue.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .