Creating the player scene¶
With the project settings in place, we can start working on the player-controlled character.
第一個場景,我們要來定義 Player
物件。把玩家做成獨立一個 Player 場景的好處是:就算遊戲其他部分還沒做好,也可以獨立測試單一場景。
節點結構¶
首先,我們需要選擇玩家物件的根節點。一般來說,場景的根節點通常對應了物件所需要的功能——也就是物件 是什麼。點擊 [其他節點] 按鈕,並新增 Area2D 節點至場景中。

Godot 會在場景樹中的節點旁邊顯示一個警告圖示。可以暫時忽略,之後再來處理。
有了 Area2D
節點,我們就能偵測跑到了玩家範圍內物件。我們先點兩下節點,把節點名稱改成 Player
。現在,場景根節點已經設定好了,接著來設定更多節點,新增更多功能。
在我們給 Player
新增任何子節點前,需要先確保不會因為點到 Player 物件而移動到 Player 或改變到大小。先選擇 Player 節點,然後點擊鎖頭右邊的圖示,滑鼠移到圖示上方的時候會顯示提示「確保物件的子級項目無法被選擇。」

接著保存場景。選擇 [場景] -> [保存],或是在 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.
Sprite 動畫¶
Click on the Player
node and add an AnimatedSprite 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 AnimatedSprite
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:

左側顯示的是動畫列表。點擊「default」(預設) 並重新命名為「walk」(走路)。接著點擊 [新增動畫] 按鈕來建立第二個動畫,將其命名為「up」(上移)。接著到 [檔案系統] 頁籤中找到玩家圖片——應該放在稍早解壓縮出來的 art
資料夾內。在動畫列表上選擇對應的動畫,並把圖片 playerGrey_up[1/2]
以及 playerGrey_walk[1/2]
拖移到 [動畫影格] 那裡 :

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

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:

完成之後, Player
看起來應該會像這樣:

修改完畢後記得保存場景。
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.