Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

playerシーンの作成

プロジェクト設定が完了したので、操作できるキャラクターを作成しましょう。

最初のシーンでは、 Player オブジェクトを定義します。Player シーンを独立して作成する利点の 1 つは、たとえゲームの他の部分を作成する前であっても、個別にテストができることです。

ノード構成

はじめに、プレイヤーオブジェクトのルートノードを選択する必要があります。原則として、シーンのルートノードはオブジェクトに望まれる機能 (オブジェクトが*何であるか*) を反映する必要があります。「その他のノード」のボタンをクリックして、シーンに Area2D <class_Area2D>` ノードを加えます。

../../_images/add_node.webp

Godotは、シーンツリーのノードの横に警告アイコンを表示します。現時点では無視して構いません。後で対処します。

Area2D を使用すると、プレイヤーに重なり合ったり、プレイヤーに衝突したりするオブジェクトを検出できます。ノードの名前をダブルクリックして、名前を Player に変更します。シーンのルートノードを決めたので、これにノードを追加して機能を追加できます。

Player ノードに子を追加する前に、追加した子を誤って移動したりサイズを変更したりしないようにします。ノードを選択し、ロックの右側にあるアイコンをクリックします。ツールチップには「選択したノードの子を選択不可にします。」と書いてあります

../../_images/lock_children.webp

シーンを保存します。 [シーン]-> [シーンを保存]をクリックするか、Windows/Linuxでは Ctrl + S を押し、macOSでは Cmd + S を押します。

注釈

このプロジェクトでは、Godotの命名規則に従います。

  • GDScript: クラス(ノード)はPascalCaseを使用し、変数と関数はsnake_caseを使用し、定数はALL_CAPSを使用します(GDScriptスタイルガイドを参照)。

  • C#: クラス、export変数、メソッドはPascalCaseを使用し、プライベートフィールドは _camelCase を使用し、ローカル変数とパラメーターは camelCase を使用します(C#スタイルガイドを参照)。シグナルを接続するときは、メソッド名を正確に入力してください。

スプライトアニメーション

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. To create one, find the Sprite Frames property under the Animation tab in the Inspector and click "[empty]" -> "New SpriteFrames". Click again 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" tab - they're in the art folder you unzipped earlier. Drag the two images for each animation, named playerGrey_up[1/2] and playerGrey_walk[1/2], into the "Animation Frames" side of the panel for the corresponding animation:

../../_images/spriteframes_panel2.webp

プレーヤーの画像はゲーム ウィンドウに対して少し大きすぎるため、縮小する必要があります。 AnimatedSprite2D ノードをクリックし、 Scale プロパティを (0.5, 0.5) に設定します。 これは、インスペクターの Node2D ヘッダの下にあります。

../../_images/player_scale.webp

最後に、 CollisionShape2DPlayer の子として追加します。 これにより、プレイヤーの「ヒットボックス」、つまり衝突領域の境界が決まります。 このキャラクターの場合、 CapsuleShape2D ノードが最適なフィット感を与えるので、インスペクターの「Shape」の横にある「[empty]」→「New CapsuleShape2D」をクリックします。 2 つのサイズ ハンドルを使用して、スプライトを覆うようにシェイプのサイズを変更します。

../../_images/player_coll_shape.webp

完了すると、 Player シーンは次のようになります:

../../_images/player_scene_nodes.webp

これらの変更後は、必ずシーンを再度保存してください。

次のパートでは、動かしたりアニメーションさせるためのスクリプトをplayerノードに追加します。それから、playerがなにかにあたった事がわかるよう、衝突判定を設定します。