02.player_scene.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. .. _doc_your_first_2d_game_player_scene:
  2. Creating the player scene
  3. =========================
  4. With the project settings in place, we can start working on the
  5. player-controlled character.
  6. The first scene will define the ``Player`` object. One of the benefits of
  7. creating a separate Player scene is that we can test it separately, even before
  8. we've created other parts of the game.
  9. Node structure
  10. ~~~~~~~~~~~~~~
  11. To begin, we need to choose a root node for the player object. As a general
  12. rule, a scene's root node should reflect the object's desired functionality -
  13. what the object *is*. Click the "Other Node" button and add an :ref:`Area2D
  14. <class_Area2D>` node to the scene.
  15. .. image:: img/add_node.webp
  16. When you add the ``Area2D`` node, Godot will display the following **warning icon**
  17. next to it in the scene tree:
  18. .. image:: img/no_shape_warning.webp
  19. This warning tells us that the ``Area2D`` node requires a shape to detect collisions or overlaps.
  20. We can **ignore the warning temporarily** because we will first set up the player's visuals
  21. (using an animated sprite). Once the visuals are ready, we will add a collision shape as a child
  22. node. This will allow us to accurately size and position the shape based on the sprite’s appearance.
  23. With ``Area2D`` we can detect objects that overlap or run into the player.
  24. Change the node's name to ``Player`` by double-clicking on it. Now that we've
  25. set the scene's root node, we can add additional nodes to give it more
  26. functionality.
  27. Before we add any children to the ``Player`` node, we want to make sure we don't
  28. accidentally move or resize them by clicking on them. Select the node and click
  29. the icon to the right of the lock. Its tooltip says "Groups the selected node
  30. with its children. This causes the parent to be selected when any child
  31. node is clicked in 2D and 3D view."
  32. .. image:: img/lock_children.webp
  33. Save the scene. Click Scene -> Save, or press :kbd:`Ctrl + S` on Windows/Linux
  34. or :kbd:`Cmd + S` on macOS.
  35. .. note:: For this project, we will be following the Godot naming conventions.
  36. - **GDScript**: Classes (nodes) use PascalCase, variables and
  37. functions use snake_case, and constants use ALL_CAPS (See
  38. :ref:`doc_gdscript_styleguide`).
  39. - **C#**: Classes, export variables and methods use PascalCase,
  40. private fields use _camelCase, local variables and parameters use
  41. camelCase (See :ref:`doc_c_sharp_styleguide`). Be careful to type
  42. the method names precisely when connecting signals.
  43. Sprite animation
  44. ~~~~~~~~~~~~~~~~
  45. Click on the ``Player`` node and add (:kbd:`Ctrl + A` on Windows/Linux or
  46. :kbd:`Cmd + A` on macOS) a child node :ref:`AnimatedSprite2D
  47. <class_AnimatedSprite2D>`. The ``AnimatedSprite2D`` will handle the
  48. appearance and animations for our player. Notice that there is a warning symbol
  49. next to the node. An ``AnimatedSprite2D`` requires a :ref:`SpriteFrames
  50. <class_SpriteFrames>` resource, which is a list of the animations it can
  51. display. To create one, find the ``Sprite Frames`` property under the ``Animation`` tab in the Inspector and click
  52. "[empty]" -> "New SpriteFrames":
  53. .. image:: img/new_spriteframes.webp
  54. Click on the ``SpriteFrames`` you just created to open the "SpriteFrames" panel:
  55. .. image:: img/spriteframes_panel.webp
  56. On the left is a list of animations. Click the "default" one and rename it to
  57. "walk". Then click the "Add Animation" button to create a second animation named
  58. "up". Find the player images in the "FileSystem" tab - they're in the ``art``
  59. folder you unzipped earlier. Drag the two images for each animation, named
  60. ``playerGrey_walk[1/2]`` and ``playerGrey_walk[2/2]``, into the "Animation Frames"
  61. side of the panel for the corresponding animation:
  62. .. image:: img/spriteframes_panel2.webp
  63. The player images are a bit too large for the game window, so we need to scale
  64. them down. Click on the ``AnimatedSprite2D`` node and set the ``Scale`` property
  65. to ``(0.5, 0.5)``. You can find it in the Inspector under the ``Node2D``
  66. heading.
  67. .. image:: img/player_scale.webp
  68. Finally, add a :ref:`CollisionShape2D <class_CollisionShape2D>` as a child of
  69. ``Player``. This will determine the player's "hitbox", or the bounds of its
  70. collision area. For this character, a ``CapsuleShape2D`` node gives the best
  71. fit, so next to "Shape" in the Inspector, click "[empty]" -> "New
  72. CapsuleShape2D". Using the two size handles, resize the shape to cover the
  73. sprite:
  74. .. image:: img/player_coll_shape.webp
  75. When you're finished, your ``Player`` scene should look like this:
  76. .. image:: img/player_scene_nodes.webp
  77. Once this is done, the warning on the ``Area2D`` node will disappear, as it now has
  78. a shape assigned and can interact with other objects.
  79. Make sure to save the scene again after these changes.
  80. In the next part, we'll add a script to the player node to move and animate it.
  81. Then, we'll set up collision detection to know when the player got hit by
  82. something.