Creating the player scene

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

第一个场景, 我们会定义 Player 对象. 单独创建Player场景的好处之一是, 在游戏的其他部分做出来之前, 我们就可以对其进行单独测试.

节点结构

首先, 我们需要为player对象选择一个根节点. 作为一般规则, 场景的根节点应该反映对象所需的功能——对象 是什么. 单击 “其他节点” 按钮并将 Area2D 节点添加到场景中.

../../_images/add_node.png

Godot将在场景树中的节点旁边显示警告图标. 你现在可以忽略它. 我们稍后再谈.

使用 Area2D 可以检测到与玩家重叠或进入玩家内的物体. 通过双击节点名称将其名称更改为 Player. 我们已经设置好了场景的根节点, 现在可以向该角色中添加其他节点来增加功能.

在将任何子级添加到 Player 节点之前, 我们要确保不会通过点击它们而意外地移动它们或调整其大小. 选择节点, 然后点击锁右侧的图标;它的工具提示显示 确保对象的子级不可选择.

../../_images/lock_children.png

保存场景。点击场景 -> 保存,或者在 Windows/Linux 平台上按下 Ctrl+S,在 macOS 上按下 Cmd+S。

注解

对于此项目, 我们将遵循Godot的命名约定.

  • GDScript : 类(节点)使用大驼峰命名法(PascalCase), 变量和函数使用蛇形命名法(snake_case), 常量使用全大写(ALL_CAPS)(请参阅 GDScript 风格指南).

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

精灵动画

Click on the Player node and add an AnimatedSprite2D node as a child. The AnimatedSprite 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. To create one, find the Frames property in the Inspector and click “[empty]“ -> “New SpriteFrames”. Click again to open the “SpriteFrames” panel:

../../_images/spriteframes_panel.png

左边是一个动画列表. 点击 “defalult” 动画, 并将其重命名为 “walk”. 然后点击 “新动画” 按钮, 创建另一个名为 “up” 的动画. 在 “文件系统” 选项卡中找到player图像-——它们应该在你之前解压的 art 文件夹中. 将每个动画的两张图像, playerGrey_up[1/2]playerGrey_walk[1/2] , 拖到对应动画的面板的 “动画帧” 处:

../../_images/spriteframes_panel2.png

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.png

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_shape.png

完成后, 你的 Player 场景看起来应该像这样:

../../_images/player_scene_nodes.png

修改完成后请确保再次保存场景.

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.