stringlibgdxbitmap-fonts

Rendering text from Array, with string stored in object using LibGdx


I have trouble making text show up (they are supposed to show up as scrolling damage numbers). If I insert a String literal as an argument to .draw, it works. Like this:

damage.fontObject.draw(game.batch,"testtest", x, y);

The scrolling also works (inside another function outside of the SpriteBatch drawing stuff).

It just won't accept the variable I store inside of the object, for some reason. Obviously I'm missing something.

in render()

    game.batch.begin();     //render begin
    renderDamageText();
    game.batch.end();       //render end

in renderDamageText()

public void renderDamageText(){

    for(int i = 0; i < damageText.size; i++){
        GameText damage = damageText.get(i);
        String showThis = damage.getText();
        float x =damage.x;
        float y =damage.y;
        damage.fontObject.draw(game.batch,showThis, x, y);
    }

}

The GameText class

public class GameText {
public MyGdxGame game;
public String text;
public float x;
public float y;
public BitmapFont fontObject;
public float alpha = 1;
public float width;
public float height;

public GameText(String text, MyGdxGame game, BitmapFont font, float x, float y){
    this.x=x;
    this.y = y;
    fontObject = font;
    this.game = game;
    this.text = text;
    setText(game, text);
}

public String getText(){
    return text;
}

public void setText(MyGdxGame game, String text){
    game.textLayout.setText(fontObject,text);
    width= game.textLayout.width;
    height = game.textLayout.height;
    x-=width/2;
    y-=height/2;
}
}

Current code is WiP and messy, just want to get it to work and then clean it up.


Solution

  • public static final String CHARACTERS_TO_MAP = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ1234567890.,:";
    

    ^This was the answer, I forgot to map the numbers ;)