Search code examples
javaandroidlibgdx

Libgdx change the image inside table dynamically


I am trying to draw a HUD that shows the score and time and image like loading but I want to update and change the image when the score get higher but I tried to do that but it is not working. I thought maybe I can define new table in the update method and put different image each time but I don't want to define new table each time. is there anyway to update the image only inside the table: this is the code:

public class Hud implements Disposable{

    public Stage stage;
    private Viewport viewport;
    private static Integer worldTimer;
    private float timecount;
    private static Integer score;
    Label countdownLabel;
    private static Label scorelabel;
    private Label timelabel;
    private Label levellabel;
    private Label worldlabel;
    private Label mariolabel;
    private Image loading;
    private Table table;
    public Hud(SpriteBatch sb)
    {
        worldTimer=40;
        timecount=0;
        score=0;
        viewport=new FitViewport(Fruits.V_WIDTH,Fruits.V_HIEGT,new OrthographicCamera());//2
        stage=new Stage(viewport,sb);//stage is as box and try to put widget and organize things inside that table
        table = new Table();
        table.top();//table at top of our stage
        table.setFillParent(true);//table is now fill all the stage
        countdownLabel=new Label(String.format("%02d",worldTimer),new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        scorelabel=new Label(String.format("%02d",score),new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        timelabel=new Label("TIME",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        levellabel=new Label("1-1",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        worldlabel=new Label("WORLD",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        mariolabel=new Label("Score"    ,new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx
        loading = new Image(new Texture("10%.png"));
        //loading.setSize(40, 10);
        table.add(mariolabel).expandX().padTop(10);

        table.add(timelabel).expandX().padTop(10);
        table.add(worldlabel).expandX().padTop(10);
        table.row();
        table.add(scorelabel).expandX();

        table.add(countdownLabel).expandX();
        table.add(loading).width(50).height(10);

        stage.addActor(table);

    }
    public void update(float dt)
    {
        timecount+=dt;
        if (timecount>1)
        {
            worldTimer--;
            if(worldTimer>=0) {
                countdownLabel.setText(String.format("%02d", worldTimer));
                loading=new Image(new Texture("90.png"));
            }
            timecount=0;

        }
    }
    public static int getTime()
    {

        return worldTimer;
    }

    public static void addScore(int value)
    {
        score +=value;
        scorelabel.setText(String.format("%02d",score));
    }
    public static int getScore()
    {
        return score;
    }
    @Override
    public void dispose() {
        stage.dispose();
    }
} 

Solution

  • You should load all the texture in your constructor instead of loading them on the fly at runtime. What you are trying to do will be easily achieved in C++ but in Java, one way to do it will be to create an array of table for the textures since that is the only thing you need to update e.g :

    private Table maintable = new Table();
    private Table loading10Table = new Table();
    private Table loading90Table = new Table();
    private Image loading10 = new Image(new Texture("10%.png"));
    private Image loading90 = new Image(new Texture("90%.png"));
    

    Add loading10 to loading10Table and loading90 to loading90Table then add mainTable and loading10Table to the stage. In the update method, assuming "loading10Table" is was inserted second in the array:

    stage.getActors()[1] = loading90Table;