当分数动态变化时,使用标签创建的分数字符串不会删除之前的分数-LibGdx

时间:2017-06-06 07:38:48

标签: java libgdx

在我的游戏用户界面中,我在盒子图像上有一个标签来写分数。

private void drawNoOfCoins() {
    Label.LabelStyle style = new Label.LabelStyle();
    style.font = game.font2;
    Label totalCoinLabel = new Label(coinScoreController.getTotalCoinString(), style);

    totalCoinLabel.setPosition(showcoinImage.getWidth() / 2,Constants.WORLD_HEIGHT - 2 * totalCoinLabel.getHeight());
    stage.addActor(totalCoinLabel);

    totalCoinLabel.setBounds( showcoinImage.getX(), showcoinImage.getY(),showcoinImage.getWidth(), showcoinImage.getHeight());
    totalCoinLabel.setAlignment( Align.center );    
}

private void ShowCoinScoreBox() {
    showcoinImage = new Image(showScoreTexture);
    showcoinImage.setPosition(Constants.WORLD_WIDTH / 42,Constants.WORLD_HEIGHT - (showcoinImage.getHeight() * 1.5f));
    stage.addActor(showcoinImage);
}

我在render()中调用此方法drawNoOfCoins(),得分正在动态更新。

但每次分数动态变化时,更新后的分数会显示在之前的分数之上。 这个问题是出于标签还是其他任何问题? 我该如何解决?

1 个答案:

答案 0 :(得分:1)

致电ShowCoinScoreBox()& drawNoOfCoins()ApplicationListener的{​​{1}}或create()接口show()方法一次Screen进行初始化。您正在渲染调用中创建新的Actor并将其添加到舞台。

在全球范围内继续引用totalCoinLabel,而不是本地。

private Label totalCoinLabel; 

private void drawNoOfCoins() {
    Label.LabelStyle style = new Label.LabelStyle();
    style.font = game.font2;
    totalCoinLabel = new Label(coinScoreController.getTotalCoinString(), style);

   ...    
}

内部render()方法更新标签文字

totalCoinLabel.setText(coinScoreController.getTotalCoinString());