Godot的生命周期方法都以下划线"_"开始,因此在内置Editor中先输入"_"即可弹出提示
extends KinematicBody2D
var speed = 0.0
var motion = Vector2()
func _ready():
# Called when the node is added to the scene for the first time.
# Initialization here
pass
func _process(delta):
if Input.is_action_just_pressed("ui_right"):
print("Input Right")
pass
if Input.is_action_pressed("ui_left"):
print("Input Left")
pass
pass
func _physics_process(delta):
#To manage the logic of a kinematic body or character,
# it is always advised to use physics process,
pass
func _input(event):
if event.is_pressed() and event.is_action_pressed("mouse_button_left"):
print("Input mouse_button_left")
pass
pass
_ready:相当于Unity中的Start用于初始化
func _ready():
# Called when the node is added to the scene for the first time.
# Initialization here
pass
_process:相当于Unity的Update,其中delta用于平衡FPS
#func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
# pass
_physics_process:类似于Unity里的FixedUpdate,貌似以前的版本就叫_fixed_process
func _physics_process(delta):
#To manage the logic of a kinematic body or character,
# it is always advised to use physics process,
pass
另外,旧版本中这两个process方法要在_ready方法中set_process(true)和set_physics_process(true)来开启调用,3.0版本以后是默认为true的
输入处理有如下两种:
1类似Unity一样放到process里在每一帧用If语句判断
2在_input里使用事件触发的回调函数
形式上第二种可以把处理输入的逻辑单独拿出来并且等到有输入事件时再执行而不用每一帧都判断一次,但是简单用了一下发现,_input类型不能区分按键持续按下的事件
func _process(delta):
if Input.is_action_just_pressed("ui_right"):
print("Input Right")
pass
if Input.is_action_pressed("ui_left"):
print("Input Left")
pass
pass
func _input(event):
if event.is_pressed() and event.is_action_pressed("mouse_button_left"):
print("Input mouse_button_left")
pass
pass