# Called when the node enters the scene tree for the first time. # 节点全部加载好后调用,所以比上面个方法晚一点,是冒泡调用 func _ready() -> void: #先调用子节点,再调用父节点 print("ready")
# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: #每帧调用 jishiqi -= delta if(jishiqi<=0): queue_free();
# Called whenv the node enters the scene tree for the first time. func _ready() -> void: #鼠标设置,显示、隐藏在游戏窗口 #Input.mouse_mode = Input.MouseMode.MOUSE_MODE_CONFINED pass # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: #TODO,这有bug #按键事件 if Input.is_key_pressed(Key.KEY_B): print("按下了B键") if Input.is_action_just_pressed("jump"): print("按下了跳跃") elif Input.is_action_pressed("jump"): print("跳跃中") elif Input.is_action_just_released("jump"): print("松开跳跃") #TODO获取按键向量 float s = Input.GetActionStrength("jump") func _input(event: InputEvent) -> void: #是键盘事件,并且按下了 if event is InputEventKey: #按下的V键,参阅 @GlobalScope_Key 以获取键码常量列表。 if event.keycode == KEY_V: #判断当前是否是持续按压 if event.is_echo(): print("持续按下了V键")
elif event.is_pressed(): print("V按下了瞬间") elif event.is_released(): print("抬起V") #鼠标事件 elif event is InputEventMouseButton: if event.is_pressed(): print(event.position) print(event.button_mask)
# Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: if Input.is_action_just_pressed("jump"): #还有个get_node()方法没演示 var childNode = get_tree().root.find_child("childNode",true,false) #childNode.queue_free(); var node2D = Node2D.new() node2D.name = "new" childNode.add_child(node2D)
获取子节点
var subNode1 = $SubNode2 var subNode2 = $”SubNode2”
获取当前节点
var currentNode1 = $”.” var currentNode2 = self
获取父节点
var parentNode1 = get_parent() var parentNode2 = $”../“
# Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: if Input.is_action_just_pressed("left"): #跳转场景1 #get_tree().change_scene_to_file("res://scen_2.tscn") #跳转场景2 #get_tree().change_scene_to_packed(newScene) #跳转场景3 get_tree().current_scene.add_child(newScene.instantiate())
# Called when the node enters the scene tree for the first time. func _ready() -> void: sprite_2d.visible = true sprite_2d.z_index = 10 sprite_2d.z_as_relative =false
# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: #获取鼠标位置 var pos = get_global_mouse_position() #看向某个点 look_at(pos) print(pos)
例2操作sprite
1 2 3 4 5 6 7 8 9 10 11 12 13
extends Sprite2D
# Called when the node enters the scene tree for the first time. func _ready() -> void: #同样也可以用导出一个属性的方式,加载图片 texture = load("res://icon.svg") centered = false # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass
# Called when the node enters the scene tree for the first time. func _ready() -> void: var enemys = get_tree().get_nodes_in_group("敌人") #TODO,没生效,作用1,分组管理 if Input.is_action_just_pressed("left"): enemys.any(queue_free) #TODO,没生效,作用2,分组消息 if Input.is_action_just_pressed("right"): get_tree().call_group("敌人","test") # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass