Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Creating the player scene

With the project settings in place, we can start working on the player-controlled character.

Pierwszą sceną, którą stworzymy, będzie Player. Jedną z zalet stworzenia oddzielnej sceny Player jest to, że możemy ją przetestować oddzielnie, nawet zanim utworzymy inne części gry.

Struktura węzłów

To begin, we need to choose a root node for the player object. As a general rule, a scene's root node should reflect the object's desired functionality - what the object is. In the upper-left corner, in the "Scene" tab, click the "Other Node" button and add an Area2D node to the scene.

Informacja

Godot also provides the CharacterBody2D node, specifically designed for 2D characters, which includes built-in support for some of the processes explained in this tutorial. In many real world projects, CharacterBody2D would be a better choice for players and enemies. However, this tutorial focuses on core concepts that apply to a wider range of nodes and use cases.

../../_images/add_node.webp

When you add the Area2D node, Godot will display the following warning icon next to it in the scene tree:

../../_images/no_shape_warning.webp

This warning tells us that the Area2D node requires a shape to detect collisions or overlaps. We can ignore the warning temporarily because we will first set up the player's visuals (using an animated sprite). Once the visuals are ready, we will add a collision shape as a child node. This will allow us to accurately size and position the shape based on the sprite's appearance.

Za pomocą Area2D możemy wykrywać obiekty, które zachodzą na siebie lub wpływają na gracza. Zmieńmy jego nazwę na Player klikając dwukrotnie na nazwę węzła. Jest to główny węzeł sceny i możemy dodać do niego dodatkowe węzły, aby zwiększyć jego funkcjonalność.

Before we add any children to the Player node, we want to make sure we don't accidentally move or resize them by clicking on them. Select the node and click the icon to the right of the lock. Its tooltip says "Groups the selected node with its children. This causes the parent to be selected when any child node is clicked in 2D and 3D view."

../../_images/lock_children.webp

Save the scene as player.tscn. Click Scene > Save, or press Ctrl + S on Windows/Linux or Cmd + S on macOS.

Informacja

Dla tego projektu, będziemy korzystać z konwencji nazewnictwa typowych dla Godota.

  • GDScript: W klasach (węzłach) używać będziemy PascalCase, w zmiennych i funkcjach snake_case, a w stałych ALL_CAPS. Wszystkie nazwy zapisywane będą bez znaków diakrytycznych (Więcej o GDScript style guide).

  • C#: Classes, export variables and methods use PascalCase, private fields use _camelCase, local variables and parameters use camelCase (See Styl pisania kodu w C#). Be careful to type the method names precisely when connecting signals.

Animacja węzła Sprite

Click on the Player node and add (Ctrl + A on Windows/Linux or Cmd + A on macOS) a child node AnimatedSprite2D. The AnimatedSprite2D will handle the appearance and animations for our player. Notice that there is a warning symbol next to the node. An AnimatedSprite2D requires a SpriteFrames resource, which is a list of the animations it can display. Make sure AnimatedSprite2D is selected and then find the Sprite Frames property under the Animation section in the Inspector and click "[empty]" -> "New SpriteFrames":

../../_images/new_spriteframes.webp

Click on the SpriteFrames you just created to open the "SpriteFrames" panel:

../../_images/spriteframes_panel.webp

On the left is a list of animations. Click the default one and rename it to walk. Then click the Add Animation button to create a second animation named up.

Find the player images in the FileSystem dock - they're in the art folder you unzipped earlier. Drag the two images for each animation, into the Animation Frames side of the panel for the corresponding animation:

  • playerGrey_walk1 and playerGrey_walk2 for the walk animation

  • playerGrey_up1 and playerGrey_up2 for the up animation

../../_images/spriteframes_panel2.webp

The player images are a bit too large for the game window, so we need to scale them down. Click on the AnimatedSprite2D node and set the Scale property to (0.5, 0.5). You can find it in the Inspector under the Node2D heading.

../../_images/player_scale.webp

Finally, add a CollisionShape2D as a child of Player. This will determine the player's "hitbox", or the bounds of its collision area. For this character, a CapsuleShape2D node gives the best fit, so next to "Shape" in the Inspector, click "[empty]" -> "New CapsuleShape2D". Using the two size handles, resize the shape to cover the sprite:

../../_images/player_coll_shape1.webp

Po zakończeniu, scena Player powinna wyglądać w ten sposób:

../../_images/player_scene_nodes.webp

Once this is done, the warning on the Area2D node will disappear, as it now has a shape assigned and can interact with other objects.

Pamiętaj o ponownym zapisaniu sceny po tych zmianach.

In the next part, we'll add a script to the player node to move and animate it. Then, we'll set up collision detection to know when the player got hit by something.