Input examples

简介

在本教程中,您将学习如何使用Godot的:ref:`输入事件<class_InputEvent>`系统捕获玩家输入。您的游戏可以使用多种不同类型的输入——键盘,游戏手柄,鼠标等等。还有许多不同的方法,将这些输入转化为游戏中的动作。本文将向您展示一些最常见的场景,您可以将其作为您自己项目的起点。

注解

要详细了解Godot的输入事件系统是如何工作的,请见:ref:文档_输入事件。

事件和轮询

有时候,你想让游戏对某个输入事件做出反应——例如按下“跳跃”按钮。对于其他情况,可能希望只要按下键就会发生一些事情,比如移动。在第一种情况下,您可以使用“_input()”函数,它将在发生输入事件时调用。在第二种情况下,Godot提供了:ref:`输入<class_Input>`单例,您可以使用它来查询输入的状态。

示例:

GDScript

C#

  1. func _input(event):
  2. if event.is_action_pressed("jump"):
  3. jump()
  4. func _physics_process(delta):
  5. if Input.is_action_pressed("move_right"):
  6. # Move as long as the key/button is pressed.
  7. position.x += speed * delta
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent.IsActionPressed("jump"))
  4. {
  5. Jump();
  6. }
  7. }
  8. public override void _PhysicsProcess(float delta)
  9. {
  10. if (Input.IsActionPressed("move_right"))
  11. {
  12. // Move as long as the key/button is pressed.
  13. position.x += speed * delta;
  14. }
  15. }

这使您能够灵活地混合和匹配所执行输入的处理类型。

对于本教程的其余部分,我们将专注于使用“_input ()”捕捉单个事件。

输入事件

输入事件是继承自:ref:`输入事件<class_InputEvent>`的对象。根据事件类型,对象将包含与该事件相关的特定属性。为了了解事件的实际情况,添加一个节点并附加以下脚本:

GDScript

C#

  1. extends Node
  2. func _input(event):
  3. print(event.as_text())
  1. using Godot;
  2. using System;
  3. public class Node : Godot.Node
  4. {
  5. public override void _Input(InputEvent inputEvent)
  6. {
  7. GD.Print(inputEvent.AsText());
  8. }
  9. }

当你按下按键,移动鼠标,并执行其他输入,您会在输出窗口中看到每个事件滚动。下面是输出的一个例子:

  1. A
  2. InputEventMouseMotion : button_mask=0, position=(108, 108), relative=(26, 1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)
  3. InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(108, 107), button_mask=1, doubleclick=false
  4. InputEventMouseButton : button_index=BUTTON_LEFT, pressed=false, position=(108, 107), button_mask=0, doubleclick=false
  5. S
  6. F
  7. Alt
  8. InputEventMouseMotion : button_mask=0, position=(108, 107), relative=(0, -1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)

如你所见,对于不同类型的输入,结果是非常不同的。按键事件甚至被打印为按键符号。例如,让我们考虑:ref:鼠标按钮输入事件<class_InputEventMouseButton>。它继承自以下类:

  • InputEvent - the base class for all input events
  • InputEventWithModifiers - adds the ability to check if modifiers are pressed, such as Shift or Alt.
  • InputEventMouse - adds mouse event properties, such as position
  • :ref:`鼠标按钮输入事件<class_InputEventMouseButton>`包含按下的按钮的索引,无论是双击,或是其他。

小技巧

在处理事件时,打开类引用是一个好主意,这样可以检查事件类型的可用属性和方法。

You can encounter errors if you try to access a property on an input type that doesn’t contain it - calling position on InputEventKey for example. To avoid this, make sure to test the event type first:

GDScript

C#

  1. func _input(event):
  2. if event is InputEventMouseButton:
  3. print("mouse button event at ", event.position)
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent is InputEventMouseButton mouseEvent)
  4. {
  5. GD.Print($"mouse button event at {mouseEvent.Position}");
  6. }
  7. }

InputMap

:ref:`事件表<class_InputMap>`是处理各种输入的最灵活的方法。您可以通过创建命名的输入*动作*来使用它,可以为它分配任意数量的输入事件,例如按键或鼠标点击。一个新的Godot项目已经包含许多默认定义操作。看看它们,然后添加你自己的,打开项目->项目设置,并选择事件表选项卡:

../../_images/inputs_inputmap.png

Capturing actions

一旦你定义了动作,可以在脚本中使用“is_action_pressed()”和“is_action_released ()”处理它们。通过名称查找响应的动作:

GDScript

C#

  1. func _input(event):
  2. if event.is_action_pressed("my_action"):
  3. print("my_action occurred!")
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent.IsActionPressed("my_action"))
  4. {
  5. GD.Print("my_action occurred!");
  6. }
  7. }

键盘事件

Keyboard events are captured in InputEventKey. While it’s recommended to use input actions instead, there may be cases where you want to specifically look at key events. For this example, let’s check for the T:

GDScript

C#

  1. func _input(event):
  2. if event is InputEventKey and event.pressed:
  3. if event.scancode == KEY_T:
  4. print("T was pressed")
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
  4. {
  5. if ((KeyList)keyEvent.Scancode == KeyList.T)
  6. {
  7. GD.Print("T was pressed");
  8. }
  9. }
  10. }

小技巧

详见:参考:“@全局范围_键位列表<枚举_@全局范围_键位列表>”获取扫描代码常量列表。

键盘修饰符

Modifier properties are inherited from InputEventWithModifiers. This allows you to check for modifier combinations using boolean properties. Let’s imagine you want one thing to happen when the T is pressed, but something different when it’s Shift + T:

GDScript

C#

  1. func _input(event):
  2. if event is InputEventKey and event.pressed:
  3. if event.scancode == KEY_T:
  4. if event.shift:
  5. print("Shift+T was pressed")
  6. else:
  7. print("T was pressed")
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
  4. {
  5. switch ((KeyList)keyEvent.Scancode)
  6. {
  7. case KeyList.T:
  8. GD.Print(keyEvent.Shift ? "Shift+T was pressed" : "T was pressed");
  9. break;
  10. }
  11. }
  12. }

小技巧

详见:参考:“@全局范围_键位列表<枚举_@全局范围_键位列表>”获取扫描代码常量列表。

鼠标事件

鼠标事件继承自:参考:’鼠标输入事件<类_鼠标输入事件>’类,并被分成两种类型::参考:’鼠标按钮输入事件<类_鼠标按钮输入事件>’和:参考:“鼠标行为输入事件<类_鼠标行为输入事件>”。注意,这意味着所有鼠标事件将包含一个“位置”(position)属性。

鼠标按钮

捕获鼠标按钮与处理按键事件非常相似。@全局作用域_按钮列表 包含每个可能按钮的“按钮_*”常量列表,它将在事件的“按钮_索引”属性中报告。注意,鼠标滚轮也可以算作一个按钮——准确地说,是两个按钮,“按钮_滚轮_上”和“按钮_滚轮_下”都是独立的事件。

GDScript

C#

  1. func _input(event):
  2. if event is InputEventMouseButton:
  3. if event.button_index == BUTTON_LEFT and event.pressed:
  4. print("Left button was clicked at ", event.position)
  5. if event.button_index == BUTTON_WHEEL_UP and event.pressed:
  6. print("Wheel up")
  1. public override void _Input(InputEvent inputEvent)
  2. {
  3. if (inputEvent as InputEventMouseButton mouseEvent && mouseEvent.Pressed)
  4. {
  5. switch ((ButtonList)mouseEvent.ButtonIndex)
  6. {
  7. case ButtonList.Left:
  8. GD.Print($"Left button was clicked at {mouseEvent.Position}");
  9. break;
  10. case ButtonList.WheelUp:
  11. GD.Print("Wheel up");
  12. break;
  13. }
  14. }
  15. }

鼠标行为

:ref:`鼠标移动输入事件<class_InputEventMouseMotion>`事件在鼠标移动时发生。可以通过“相对”属性找到移动的距离。

下面是一个使用鼠标事件拖放:ref:`精灵<class_Sprite>`节点的例子:

GDScript

C#

  1. extends Node
  2. var dragging = false
  3. var click_radius = 32 # Size of the sprite.
  4. func _input(event):
  5. if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
  6. if (event.position - $Sprite.position).length() < click_radius:
  7. # Start dragging if the click is on the sprite.
  8. if not dragging and event.pressed:
  9. dragging = true
  10. # Stop dragging if the button is released.
  11. if dragging and not event.pressed:
  12. dragging = false
  13. if event is InputEventMouseMotion and dragging:
  14. # While dragging, move the sprite with the mouse.
  15. $Sprite.position = event.position
  1. using Godot;
  2. using System;
  3. public class Node2D : Godot.Node2D
  4. {
  5. private bool dragging = false;
  6. private int clickRadius = 32; // Size of the sprite.
  7. public override void _Input(InputEvent inputEvent)
  8. {
  9. Sprite sprite = GetNodeOrNull<Sprite>("Sprite");
  10. if (sprite == null)
  11. {
  12. return; // No suitable node was found.
  13. }
  14. if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
  15. {
  16. if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
  17. {
  18. // Start dragging if the click is on the sprite.
  19. if (!dragging && mouseEvent.Pressed)
  20. {
  21. dragging = true;
  22. }
  23. }
  24. // Stop dragging if the button is released.
  25. if (dragging && !mouseEvent.Pressed)
  26. {
  27. dragging = false;
  28. }
  29. }
  30. else
  31. {
  32. if (inputEvent is InputEventMouseMotion motionEvent && dragging)
  33. {
  34. // While dragging, move the sprite with the mouse.
  35. sprite.Position = motionEvent.Position;
  36. }
  37. }
  38. }
  39. }

Touch events

如果使用触摸屏设备,可以生成触摸事件。:ref:`触摸屏幕输入事件<class_InputEventScreenTouch>`相当于鼠标点击事件,并且:ref:`拖拽屏幕输入事件<class_InputEventScreenDrag>`的工作原理与鼠标移动非常相似。

小技巧

要在非触摸屏设备上测试触摸事件,打开项目设置,进入“输入设备/指向”部分。启用“模拟鼠标触摸”,您的项目将把鼠标单击和移动解释为触摸事件。