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

编程入门 行业动态 更新时间:2024-10-18 20:27:59
分数动态变化时,使用标签创建的分数字符串不会删除先前分数-LibGdx(score string created with label not erasing previous score while score changes dynamically-LibGdx)

在我的游戏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());

更多推荐

本文发布于:2023-08-07 01:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分数   字符串   先前   标签   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!