Libgdx动态更改表内的图像

时间:2016-06-11 21:15:01

标签: java android libgdx

我正在尝试绘制一个HUD,显示分数,时间和图像,如加载,但我想更新和更改图像,当分数越来越高,但我试图这样做,但它不起作用。我想也许我可以在更新方法中定义新表并且每次都放置不同的图像但我不想每次都定义新表。无论如何只在表格内更新图像: 这是代码:

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();
    }
} 

1 个答案:

答案 0 :(得分:0)

您应该在构造函数中加载所有纹理,而不是在运行时动态加载它们。你想要做的事情很容易在C ++中实现,但在Java中,一种方法是为纹理创建一个表数组,因为这是你需要更新的唯一内容,例如:

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"));

loading10添加到loading10Table,将loading90添加到loading90Table,然后将mainTableloading10Table添加到舞台。在更新方法中,假设" loading10Table"是在数组中插入第二个:

stage.getActors()[1] = loading90Table;