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

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()
主站蜘蛛池模板: 卢龙县| 贵溪市| 靖江市| 南皮县| 霍山县| 苗栗县| 衡南县| 合山市| 堆龙德庆县| 莒南县| 盐源县| 平乡县| 穆棱市| 永康市| 宁河县| 尼勒克县| 梨树县| 邵阳市| 新干县| 寿阳县| 莱芜市| 汤阴县| 延津县| 罗城| 阿拉尔市| 察哈| 长兴县| 新竹市| 任丘市| 无锡市| 双流县| 碌曲县| 临清市| 楚雄市| 宿松县| 永丰县| 龙州县| 蓬莱市| 思茅市| 五大连池市| 龙岩市|