02.player_scene.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.png
  16. Godot will display a warning icon next to the node in the scene tree. You can
  17. ignore it for now. We will address it later.
  18. With ``Area2D`` we can detect objects that overlap or run into the player.
  19. Change the node's name to ``Player`` by double-clicking on it. Now that we've
  20. set the scene's root node, we can add additional nodes to give it more
  21. functionality.
  22. Before we add any children to the ``Player`` node, we want to make sure we don't
  23. accidentally move or resize them by clicking on them. Select the node and click
  24. the icon to the right of the lock; its tooltip says "Makes sure the object's
  25. children are not selectable."
  26. .. image:: img/lock_children.png
  27. Save the scene. Click Scene -> Save, or press :kbd:`Ctrl + S` on Windows/Linux
  28. or :kbd:`Cmd + S` on macOS.
  29. .. note:: For this project, we will be following the Godot naming conventions.
  30. - **GDScript**: Classes (nodes) use PascalCase, variables and
  31. functions use snake_case, and constants use ALL_CAPS (See
  32. :ref:`doc_gdscript_styleguide`).
  33. - **C#**: Classes, export variables and methods use PascalCase,
  34. private fields use _camelCase, local variables and parameters use
  35. camelCase (See :ref:`doc_c_sharp_styleguide`). Be careful to type
  36. the method names precisely when connecting signals.
  37. Sprite animation
  38. ~~~~~~~~~~~~~~~~
  39. Click on the ``Player`` node and add an :ref:`AnimatedSprite
  40. <class_AnimatedSprite>` node as a child. The ``AnimatedSprite`` will handle the
  41. appearance and animations for our player. Notice that there is a warning symbol
  42. next to the node. An ``AnimatedSprite`` requires a :ref:`SpriteFrames
  43. <class_SpriteFrames>` resource, which is a list of the animations it can
  44. display. To create one, find the ``Frames`` property in the Inspector and click
  45. "[empty]" -> "New SpriteFrames". Click again to open the "SpriteFrames" panel:
  46. .. image:: img/spriteframes_panel.png
  47. On the left is a list of animations. Click the "default" one and rename it to
  48. "walk". Then click the "New Animation" button to create a second animation named
  49. "up". Find the player images in the "FileSystem" tab - they're in the ``art``
  50. folder you unzipped earlier. Drag the two images for each animation, named
  51. ``playerGrey_up[1/2]`` and ``playerGrey_walk[1/2]``, into the "Animation Frames"
  52. side of the panel for the corresponding animation:
  53. .. image:: img/spriteframes_panel2.png
  54. The player images are a bit too large for the game window, so we need to scale
  55. them down. Click on the ``AnimatedSprite`` node and set the ``Scale`` property
  56. to ``(0.5, 0.5)``. You can find it in the Inspector under the ``Node2D``
  57. heading.
  58. .. image:: img/player_scale.png
  59. Finally, add a :ref:`CollisionShape2D <class_CollisionShape2D>` as a child of
  60. ``Player``. This will determine the player's "hitbox", or the bounds of its
  61. collision area. For this character, a ``CapsuleShape2D`` node gives the best
  62. fit, so next to "Shape" in the Inspector, click "[empty]"" -> "New
  63. CapsuleShape2D". Using the two size handles, resize the shape to cover the
  64. sprite:
  65. .. image:: img/player_coll_shape.png
  66. When you're finished, your ``Player`` scene should look like this:
  67. .. image:: img/player_scene_nodes.png
  68. Make sure to save the scene again after these changes.
  69. In the next part, we'll add a script to the player node to move and animate it.
  70. Then, we'll set up collision detection to know when the player got hit by
  71. something.