0
\$\begingroup\$

In my libGDX app I have some text running on the main Game class, and when I switch to a different screen the text is still there? Can anyone explain this please. Also in my main class it is changing screens to a splash screen then to the screen I mentioned earlier. But the splash screen doesn't have the text on the top.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Gdx.gl.glClear : as .glClear suggests, this function clears the screen before drawing the next frame. It depends where do you instantiate your font object (as you're using libgdx I guess you use BitmapFont to create your font object).

An approach could be to instantiate it in the main class (the one that extends Game), and whenever you want to draw it on the screen, you call it in the render() method.

enter image description here

Assuming you're using BitmapFont to draw your text :

MainClass

private BitmapFont font;
private SomeScreen someScreen;

    @Override
    public void create () {
        font = new BitmapFont (path); 
        someScreen = new SomeScreen (this);
        (...)
        setScreen(someScreen);
    }

    public void drawText (String text) { 
      font.draw (spritebatch, text, x, y);
    }

SomeScreen

 public class SomeScreen implements Screen {

    // create reference to the main class
    MainClass main;

   // the contructor
   public SomeScreen (MainClass main) { 
   this.main = main;
   }

   @Override
   public void render(float delta) {
    // clean the screen
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    spritebatch.begin();
    main.drawText("your text here");
    spritebatch.end();
    }


}

It can be also drawn during the main life-cycle :

MainClass

  (...)
    @Override
    public void render () { 
     super.render(); // it draws whatever is in the render() method of the current screen
     font.drawText ("your text here"); // and then you draw your text (actually it doesn't make much sense)
    }
  (...)
\$\endgroup\$
-1
\$\begingroup\$

Try adding Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); to your render function, as still clears the buffer, and alloys the new screen to load.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Elaborate on why this would fix the issue. This is a pretty short answer without a description. \$\endgroup\$ Dec 8, 2014 at 15:00

You must log in to answer this question.

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