ube*_*lut 3 game-engine gdscript godot
所以我设置了 Q 和 E 来控制固定在 8 个方向的相机。问题是当我调用 Input.is_action_just_pressed() 时,它设置为 true 两次,因此它的内容执行两次。
这就是它对计数器所做的事情:
0 0 0 0 1 1 2 2 2 2
我该如何修复?
if Input.is_action_just_pressed("camera_right", true):
if cardinal_count < cardinal_index.size() - 1:
cardinal_count += 1
else:
cardinal_count = 0
emit_signal("cardinal_count_changed", cardinal_count)
Run Code Online (Sandbox Code Playgroud)
The*_*aot 11
_process或_physics_process_process如果您的代码在或中运行,则您的代码应该可以正常工作 - 无需报告两次_physics_process。
这是因为如果在当前帧中按下is_action_just_pressed该操作,则会返回。默认情况下,这意味着图形框架,但该方法实际上检测它是在物理框架还是图形框架中调用,正如您在其源代码中看到的那样。根据设计,每个图形帧只能调用一次,每个物理帧只能调用一次。_process_physics_process
_input但是,如果您在 中运行代码_input,请记住您将为每个输入事件调用_input。并且一个框架中可以有多个。因此,可以多次调用_inputwhere is_action_just_pressed。这是因为它们位于同一框架(图形框架,这是默认的)中。
现在,让我们看看建议的解决方案(来自评论):
if event is InputEventKey:
if Input.is_action_just_pressed("camera_right", true) and not event.is_echo():
# whatever
pass
Run Code Online (Sandbox Code Playgroud)
它正在测试"camera_right"当前图形帧中是否按下了该动作。但它可能是与当前正在处理的输入事件不同的输入事件 ( event)。
因此,你可以欺骗这段代码。按配置为的键"camera_right"同时这正是我们试图避免的。
为了正确避免这种情况,您需要检查当前是否event是您感兴趣的操作。您可以使用 来执行此操作event.is_action("camera_right")。现在,你有一个选择。你可以这样做:
if event.is_action("camera_right") and event.is_pressed() and not event.is_echo():
# whatever
pass
Run Code Online (Sandbox Code Playgroud)
这就是我的建议。它检查它是否是正确的操作,是否是按下(而不是释放)事件,并且不是回显(形成键盘重复)。
或者你可以这样做:
if event.is_action("camera_right") and Input.is_action_just_pressed("camera_right", true) and not event.is_echo():
# whatever
pass
Run Code Online (Sandbox Code Playgroud)
我不建议这样做,因为:首先,它更长;其次,is_action_just_pressed确实不适合在_input. 因为is_action_just_pressed它与框架的概念相关。的设计is_action_just_pressed旨在与_processor _physics_process、 NOT 一起_input使用。