官术网_书友最值得收藏!

Updating UI via GDScript

Add a script to the HUD node. This script will update the UI elements when their properties need to change, updating the score text whenever a coin is collected, for example. Refer to the following code:

extends CanvasLayer

signal start_game

func update_score(value):
$MarginContainer/ScoreLabel.text = str(value)

func update_timer(value):
$MarginContainer/TimeLabel.text = str(value)

The Main scene's script will call these functions to update the display whenever there is a change in value. For the MessageLabel, you also need a timer to make it disappear after a brief period. Add a Timer node and change its name to MessageTimerIn the Inspector, set its Wait Time to 2 seconds and check the box to set One Shot to On. This ensures that, when started, the timer will only run once, rather than repeating. Add the following code:

func show_message(text):
$MessageLabel.text = text
$MessageLabel.show()
$MessageTimer.start()

In this function, you display the message and start the timer. To hide the message, connect the timeout() signal of MessageTimer and add this:

func _on_MessageTimer_timeout():
$MessageLabel.hide()
主站蜘蛛池模板: 渭源县| 方城县| 安康市| 沙坪坝区| 进贤县| 丹凤县| 邮箱| 灵山县| 红桥区| 郯城县| 永靖县| 手游| 新绛县| 博客| 抚远县| 隆回县| 平顶山市| 东兴市| 中超| 集贤县| 东安县| 婺源县| 嘉禾县| 阜阳市| 东山县| 遂川县| 南京市| 西丰县| 漳平市| 抚松县| 常州市| 麦盖提县| 昭苏县| 金川县| 德庆县| 含山县| 安塞县| 永川市| 金寨县| 芦山县| 阜新|