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

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()
主站蜘蛛池模板: 阿拉善盟| 石台县| 南汇区| 文登市| 明星| 涡阳县| 内江市| 肃南| 偏关县| 确山县| 得荣县| 花莲县| 珲春市| 武平县| 沈阳市| 高淳县| 仙居县| 泉州市| 邵阳县| 西安市| 阿瓦提县| 南阳市| 平塘县| 安陆市| 泸定县| 钦州市| 陆丰市| 秭归县| 沾化县| 湖南省| 广平县| 马尔康县| 贵阳市| 嘉善县| 嘉峪关市| 余庆县| 长春市| 越西县| 万载县| 八宿县| 淮安市|