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

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()
主站蜘蛛池模板: 麦盖提县| 黄冈市| 鸡泽县| 揭阳市| 江阴市| 天台县| 彭泽县| 拜泉县| 从江县| 乌拉特中旗| 平阴县| 铁力市| 呼和浩特市| 丘北县| 顺平县| 烟台市| 游戏| 噶尔县| 江津市| 洮南市| 临湘市| 安国市| 新巴尔虎左旗| 桃源县| 将乐县| 三亚市| 望都县| 石楼县| 泉州市| 凤阳县| 光泽县| 老河口市| 天门市| 和林格尔县| 麻江县| 闸北区| 当涂县| 花莲县| 南乐县| 平山县| 金山区|