在我的游戏UI中,我在盒子图像上有一个标签来写分数。
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()并且得分正在动态更新。
但每次分数动态变化时,更新的分数将显示在之前的分数之上。 这个问题是出于标签还是其他任何问题? 我该如何解决?
In my game UI, I have a label on a box image to write score.
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); }I am calling this method drawNoOfCoins() in render() and score is getting updated dynamically.
But every time the score changes dynamically,updated score is showing on top of the previous score. Is this problem comes because of label or any other things? How can I solve it?
最满意答案
从ApplicationListener的create()或Screen Interface的show()方法立即调用ShowCoinScoreBox() & drawNoOfCoins()进行初始化。 您正在渲染调用中创建新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());Call ShowCoinScoreBox() & drawNoOfCoins() at once from ApplicationListener's create() or from show() method of Screen Interface for initialization. You're creating new Actor on render call and adding them to the stage.
Keep reference of totalCoinLabel globally instead of local.
private Label totalCoinLabel; private void drawNoOfCoins() { Label.LabelStyle style = new Label.LabelStyle(); style.font = game.font2; totalCoinLabel = new Label(coinScoreController.getTotalCoinString(), style); ... }Inside render() method update label text
totalCoinLabel.setText(coinScoreController.getTotalCoinString());更多推荐
发布评论