mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 19:29:06 +00:00
Add input mapping to creeps tutorial (#5383)
* Add input mapping to creeps tutorial
This commit is contained in:
@@ -139,8 +139,36 @@ First, we need to check for input - is the player pressing a key? For this game,
|
||||
we have 4 direction inputs to check. Input actions are defined in the Project
|
||||
Settings under "Input Map". Here, you can define custom events and assign
|
||||
different keys, mouse events, or other inputs to them. For this game, we will
|
||||
just use the default events called "ui_right" etc that are assigned to the arrow
|
||||
keys on the keyboard.
|
||||
map the arrow keys to the four directions.
|
||||
|
||||
Click on *Project -> Project Settings* to open the project settings window and
|
||||
click on the *Input Map* tab at the top. Type "move_right" in the top bar and
|
||||
click the "Add" button to add the ``move_right`` action.
|
||||
|
||||
.. image:: img/input-mapping-add-action.png
|
||||
|
||||
We need to assign a key to this action. Click the "+" icon on the right, then
|
||||
click the "Key" option in the drop-down menu. A dialog asks you to type in the
|
||||
desired key. Press the right arrow on your keyboard and click "Ok".
|
||||
|
||||
.. image:: img/input-mapping-add-key.png
|
||||
|
||||
Repeat these steps to add three more mappings:
|
||||
|
||||
1. ``move_left`` mapped to the left arrow key.
|
||||
2. ``move_up`` mapped to the up arrow key.
|
||||
3. And ``move_down`` mapped to the down arrow key.
|
||||
|
||||
Your input map tab should look like this:
|
||||
|
||||
.. image:: img/input-mapping-completed.png
|
||||
|
||||
Click the "Close" button to close the project settings.
|
||||
|
||||
.. note::
|
||||
|
||||
We only mapped one key to each input action, but you can map multiple keys,
|
||||
joystick buttons, or mouse buttons to the same input action.
|
||||
|
||||
You can detect whether a key is pressed using ``Input.is_action_pressed()``,
|
||||
which returns ``true`` if it's pressed or ``false`` if it isn't.
|
||||
@@ -150,13 +178,13 @@ which returns ``true`` if it's pressed or ``false`` if it isn't.
|
||||
|
||||
func _process(delta):
|
||||
var velocity = Vector2.ZERO # The player's movement vector.
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
if Input.is_action_pressed("move_right"):
|
||||
velocity.x += 1
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
if Input.is_action_pressed("move_left"):
|
||||
velocity.x -= 1
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
if Input.is_action_pressed("move_down"):
|
||||
velocity.y += 1
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
if Input.is_action_pressed("move_up"):
|
||||
velocity.y -= 1
|
||||
|
||||
if velocity.length() > 0:
|
||||
@@ -171,22 +199,22 @@ which returns ``true`` if it's pressed or ``false`` if it isn't.
|
||||
{
|
||||
var velocity = Vector2.Zero; // The player's movement vector.
|
||||
|
||||
if (Input.IsActionPressed("ui_right"))
|
||||
if (Input.IsActionPressed("move_right"))
|
||||
{
|
||||
velocity.x += 1;
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("ui_left"))
|
||||
if (Input.IsActionPressed("move_left"))
|
||||
{
|
||||
velocity.x -= 1;
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("ui_down"))
|
||||
if (Input.IsActionPressed("move_down"))
|
||||
{
|
||||
velocity.y += 1;
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("ui_up"))
|
||||
if (Input.IsActionPressed("move_up"))
|
||||
{
|
||||
velocity.y -= 1;
|
||||
}
|
||||
|
||||
@@ -45,16 +45,23 @@ Since the game is played with keyboard controls, it would be convenient if we
|
||||
could also start the game by pressing a key on the keyboard. We can do this with
|
||||
the "Shortcut" property of the ``Button`` node.
|
||||
|
||||
In a previous lesson, we created four input actions to move the character. We
|
||||
will create a similar input action to map to the start button.
|
||||
|
||||
Select "Project" -> "Project Settings" and then click on the "Input Map"
|
||||
tab. In the same way you created the movement input actions, create a new
|
||||
input action called ``start_game`` and add a key mapping for the :kbd:`Enter`
|
||||
key.
|
||||
|
||||
In the ``HUD`` scene, select the ``StartButton`` and find its *Shortcut*
|
||||
property in the Inspector. Select "New Shortcut" and click on the "Shortcut"
|
||||
item. A second *Shortcut* property will appear. Select "New InputEventAction"
|
||||
and click the new "InputEventAction". Finally, in the *Action* property, type
|
||||
the name ``ui_select``. This is the default input event associated with the
|
||||
spacebar.
|
||||
the name ``start_game``.
|
||||
|
||||
.. image:: img/start_button_shortcut.png
|
||||
|
||||
Now when the start button appears, you can either click it or press :kbd:`Space`
|
||||
Now when the start button appears, you can either click it or press :kbd:`Enter`
|
||||
to start the game.
|
||||
|
||||
And with that, you completed your first 2D game in Godot.
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 33 KiB |
Reference in New Issue
Block a user