func _input(event)
中传入的参数event
是一个InputEvent
类型,这个类型没有直接判断“刚刚”发生的方法,所以要如下判断:
e.is_action_pressed(action) and not e.is_echo()
可以把这段代码写成一个静态的帮助类
#InputHelper.gd
class_name InputHelper
static func is_action_pressed(e :InputEvent,action:String):
return e.is_action_pressed(action)
static func is_action_just_pressed(e :InputEvent,action:String):
return e.is_action_pressed(action) and not e.is_echo()
这样我们使用的时候只要:
func _input(event):
if InputHelper.is_action_just_pressed(event):
...
pass