0
\$\begingroup\$

I want to keep the score visible on the top left corner of the screen. I have the score talking care of. I just want to display it.

Score: x

The comment on the accepted answer on Stackoverflow suggests that BitmapFontCache is the best way to do it, inside the render method.

  • BitmapFontCache.draw() doesn't accept strings, so I cannot pass Score:
  • BitmapFontCache.draw(batch, x, Align.topLeft); doesn't work either.
  • Do I have to keep updating the score position?

EDIT

  game.batch.begin();
        cache.setText("Score: " + score, 500,500);
        cache.draw(game.batch);

.........

or even if I placed it outside batch.begin, if I place it directly in, I get

E/AndroidRuntime: FATAL EXCEPTION: GLThread 172645
                  Process: com.mygdx.game, PID: 16727
                  java.lang.NullPointerException: Attempt to invoke virtual method 'com.badlogic.gdx.graphics.g2d.GlyphLayout com.badlogic.gdx.graphics.g2d.BitmapFontCache.setText(java.lang.CharSequence, float, float)' on a null object reference
                      at com.mygdx.game.GameScreen.render(GameScreen.java:129)
                      at com.badlogic.gdx.Game.render(Game.java:46)
                      at com.mygdx.game.Gam.render(Gam.java:20)
                      at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
                      at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1647)
                      at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1352)
E/AndroidGraphics: waiting for pause synchronization took too long; assuming deadlock and killing

Even if I remove score variable and put everything outside batch.begin but inside render I keep getting the same error.

 public void render(float delta) {
        cache.setText("Score: " + 12, 500,500);
        cache.draw(game.batch);
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$
  1. To set the text you have to use

    BitmapFontCache.setText(text, x, y);

  2. In the render method you just do

    BitmapFontCache.draw(batch);

  3. You only have to update the score position once in the render method.
\$\endgroup\$
12
  • \$\begingroup\$ and how to pass the int x \$\endgroup\$
    – Lynob
    Feb 18, 2017 at 0:40
  • \$\begingroup\$ varx = 22; BitmapFontCache.setText("score: "+varx, x,y); \$\endgroup\$
    – Klemmensen
    Feb 18, 2017 at 0:41
  • \$\begingroup\$ I'm getting ` java.lang.NullPointerException: Attempt to invoke virtual method 'com.badlogic.gdx.graphics.g2d.GlyphLayout com.badlogic.gdx.graphics.g2d.BitmapFontCache.setText(java.lang.CharSequence, float, float)' on a null object reference` \$\endgroup\$
    – Lynob
    Feb 18, 2017 at 0:54
  • \$\begingroup\$ why would that happen \$\endgroup\$
    – Lynob
    Feb 18, 2017 at 0:54
  • \$\begingroup\$ Try paste the code that make it a lot easier. \$\endgroup\$
    – Klemmensen
    Feb 18, 2017 at 1:00
0
\$\begingroup\$

Thanks for Klemmensen I ended up doing

game.font.setColor(Color.WHITE);
game.font.draw(game.batch, "Score: " +score, x, y);
\$\endgroup\$

You must log in to answer this question.

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