Godot 遊戲引擎指南/操作場景樹
外觀
因此,您可以在編輯器中新增、重新命名、移動和刪除節點。但是如何使用程式碼來操作呢?這是一個非常重要的操作,但並不明顯該如何實現。
以下是如何建立子彈、將其新增到場景中,並在 10 秒後刪除它。
- player.gd
extends KinematicBody2D
func _physics_process(delta):
if Input.is_action_just_pressed("shoot"):
# Create bullet and add it as a child of its parent:
var bullet=preload("res://bullet.tscn").instance()
get_parent().add_child(bullet)
- bullet.gd
extends Area2D
var timer:=0.0
var dir = 1 # Right = 1, Left = -1
func _physics_process(delta):
# Increase the timer
timer+=delta
if timer >= 10:
# Ten seconds have passed - delete on next frame
queue_free()
position.x+=(5 * dir) *delta
queue_free(): 在下一幀或節點指令碼執行完畢後刪除節點。
add_child(node: Node): 將 node 作為呼叫 add_child() 的節點的子節點新增,前提是 node 還沒有父節點。
如何關閉您的遊戲?如何正確地暫停/恢復您的遊戲?
# Remember: call "get_tree()" before calling any of these
# Quit the game/app
get_tree().quit()
# Pause
get_tree().paused=true # Or "false" to unpause
# Pausing stops "_physics_process()".
# It also stops "_process()" and "*_input()" if pause mode is not "pause_mode_process" for the node.

