敵の作成¶
次はプレイヤーが避けるべき敵を作りましょう。敵の行動はあまり複雑ではありません。モブは画面の端でランダムに生まれると、ランダムな方向を選び、一直線に進みます。
これから Mob
シーンを作り、それをインスタンス化して、ゲーム内に任意の数の独立したモブを作成します。
ノードの設定¶
[シーン → 新規シーン] をクリックして、次のノードを追加します:
RigidBody2D(
Mob
という名前)
Playerシーンと同様に、選択できないように子を設定することを忘れないでください。
RigidBody2Dプロパティで、 Gravity Scale
を 0
に設定して、モブが下方に落ちないようにします。さらに、 CollisionObject2D セクションの下にある Mask
プロパティをクリックし、最初のチェックボックスをオフにします。これにより、モブが互いに衝突しないようになります。

プレイヤーに対して行ったように AnimatedSprite を設定します。 今回は、3つのアニメーションがあります: fly
、swim
、walk
です。 artフォルダ内には、各アニメーション用の画像が2枚あります。
全てのアニメーションの "Speed (FPS)" を 3
に調整してください。

インスペクタの Playing
プロパティを「オン」に設定します。
モブにバラエティを持たせるために、1つのアニメーションをランダムに選択します。
プレイヤーの画像と同様に、これらのモブ画像は縮小する必要があります。 AnimatedSprite
の Scale
プロパティを (0.75, 0.75)
に設定します。
Player
シーンと同様に、コリジョンに CapsuleShape2D
を追加します。図形を画像に合わせるには、Rotation Degrees
プロパティを 90
に設定する必要があります (インスペクタの「Transform」の下にあります)。
シーンを保存します。
Enemyスクリプト¶
以下のように``Mob`` にスクリプトを追加します。:
extends RigidBody2D
public class Mob : RigidBody2D
{
// Don't forget to rebuild the project.
}
// Copy `player.gdns` to `mob.gdns` and replace `Player` with `Mob`.
// Attach the `mob.gdns` file to the Mob node.
// Create two files `mob.cpp` and `mob.hpp` next to `entry.cpp` in `src`.
// This code goes in `mob.hpp`. We also define the methods we'll be using here.
#ifndef MOB_H
#define MOB_H
#include <AnimatedSprite.hpp>
#include <Godot.hpp>
#include <RigidBody2D.hpp>
class Mob : public godot::RigidBody2D {
GODOT_CLASS(Mob, godot::RigidBody2D)
godot::AnimatedSprite *_animated_sprite;
public:
void _init() {}
void _ready();
void _on_VisibilityNotifier2D_screen_exited();
static void _register_methods();
};
#endif // MOB_H
それでは、スクリプトの残りの部分を見てみましょう。 _ready()
では、アニメーションを再生し、3つのアニメーションタイプのいずれかをランダムに選択します:
func _ready():
$AnimatedSprite.playing = true
var mob_types = $AnimatedSprite.frames.get_animation_names()
$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]
public override void _Ready()
{
var animSprite = GetNode<AnimatedSprite>("AnimatedSprite");
animSprite.Playing = true;
string[] mobTypes = animSprite.Frames.GetAnimationNames();
animSprite.Animation = mobTypes[GD.Randi() % mobTypes.Length];
}
// This code goes in `mob.cpp`.
#include "mob.hpp"
#include <RandomNumberGenerator.hpp>
#include <SpriteFrames.hpp>
void Mob::_ready() {
godot::Ref<godot::RandomNumberGenerator> random = godot::RandomNumberGenerator::_new();
random->randomize();
_animated_sprite = get_node<godot::AnimatedSprite>("AnimatedSprite");
_animated_sprite->_set_playing(true);
godot::PoolStringArray mob_types = _animated_sprite->get_sprite_frames()->get_animation_names();
_animated_sprite->set_animation(mob_types[random->randi() % mob_types.size()]);
}
まず、アニメーション名のリストを AnimatedSprite の frames
プロパティから取得します。これは3つのアニメーション名をすべて含む配列を返します: ["walk", "swim", "fly"]
。
次に、 0
から 2
の間の乱数を選んで、リストから名前を選ぶ必要があります (配列のインデックスは 0
から始まります)。 randi() % n
は 0
から n-1
の間の乱数を選びます。
注釈
シーンを実行するたびに「ランダム」な数字のシーケンスを異なるようにするには、 randomize()
を使用する必要があります。 Main
シーンで randomize()
を使用するので、ここでは必要はありません。
最後のピースは、モブが画面を離れたときにモブ自身を削除することです。 VisibilityNotifier2D
ノードの screen_exited()
シグナルを接続し、次のコードを追加します:
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
public void OnVisibilityNotifier2DScreenExited()
{
QueueFree();
}
// This code goes in `mob.cpp`.
void Mob::_on_VisibilityNotifier2D_screen_exited() {
queue_free();
}
これで Mob シーンが完成します。
プレイヤーと敵が準備できたので、次のパートではそれらを新しいシーンでまとめます。ランダムに敵が出現、まっすぐ動くようにし、プロジェクトを遊べるゲームにします。