Creating the enemy

是时候去做一些玩家必须躲避的敌人了. 它们的行为很简单: 怪物将随机生成在屏幕的边缘, 沿着随机的方向直线移动. Mob 的怪物场景, 以便在游戏中独立 实例化 出任意数量的怪物.

节点设置

点击场景 -> 新建场景然后添加以下节点:

  • RigidBody2D (名为 Mob)
    • AnimatedSprite
  • CollisionShape2D
  • VisibilityNotifier2D Player 场景所做的那样. RigidBody2D 属性中, 将 Gravity Scale 设置为 0, 这样怪物就不会下坠. 此外, 在 PhysicsBody2D 部分下, 点击 Mask 属性并去除第一个复选框的勾选. 这会确保怪物不会彼此碰撞. AnimatedSprite。这一次,我们有3个动画:flyswimwalk,每个动画在 art 文件夹中都有 2 张图片。 3 . Playing property in the Inspector to “On”. 我们将随机选择其中一个动画, 以便mobs有一些变化. AnimatedSprite2D‘s Scale property to (0.75, 0.75). Player 场景中一样, 为碰撞添加一个 CapsuleShape2D . 为了使形状与图像对齐, 您需要将 Rotation Degrees (在属性检查器的 “Transorm” 下)属性设置为 90. 保存该场景.

    敌人的脚本

    Mob like this: GDScript C# C++
    1. extends RigidBody2D
    1. public class Mob : RigidBody2D{ // Don't forget to rebuild the project.}
    1. // 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 <AnimatedSprite2D.hpp>#include <Godot.hpp>#include <RigidBody2D.hpp>class Mob : public godot::RigidBody2D { GODOT_CLASS(Mob, godot::RigidBody2D) godot::AnimatedSprite2D *_animated_sprite;public: void _init() {} void _ready(); void _on_VisibilityNotifier2D_screen_exited(); static void _register_methods();};#endif // MOB_H
    _ready() we play the animation and randomly choose one of the three animation types: GDScript C# C++
    1. func _ready(): $AnimatedSprite2D.playing = true var mob_types = $AnimatedSprite2D.frames.get_animation_names() $AnimatedSprite2D.animation = mob_types[randi() % mob_types.size()]
    1. public override void _Ready(){ var animSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D"); animSprite2D.Playing = true; string[] mobTypes = animSprite2D.Frames.GetAnimationNames(); animSprite2D.Animation = mobTypes[GD.Randi() % mobTypes.Length];}
    1. // 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::AnimatedSprite2D>("AnimatedSprite2D"); _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()]);}
    frames property. This returns an Array containing all three animation names: ["walk", "swim", "fly"]. 02 之间选取一个随机的数字, 以在列表中选择一个名称(数组索引以 0 起始). randi() % n 会在 0 and n-1 之中选择一个随机整数. 注解 randomize() . 我们将在 Main 场景中使用 randomize() , 因此在这里不需要添加. VisibilityNotifier2D 节点的 screen_exited() 信号并添加以下代码: GDScript C# C++
    1. func _on_VisibilityNotifier2D_screen_exited(): queue_free()
    1. public void OnVisibilityNotifier2DScreenExited(){ QueueFree();}
    1. // This code goes in `mob.cpp`.void Mob::_on_VisibilityNotifier2D_screen_exited() { queue_free();}
    Mob 场景. With the player and enemies ready, in the next part, we’ll bring them together in a new scene. We’ll make enemies spawn randomly around the game board and move forward, turning our project into a playable game.