Creating the enemy
是时候去做一些玩家必须躲避的敌人了. 它们的行为很简单: 怪物将随机生成在屏幕的边缘, 沿着随机的方向直线移动.
Mob 的怪物场景, 以便在游戏中独立 实例化 出任意数量的怪物.
节点设置
点击场景 -> 新建场景然后添加以下节点:
- RigidBody2D (名为
Mob)- AnimatedSprite
- CollisionShape2D
- VisibilityNotifier2D
Player场景所做的那样. RigidBody2D 属性中, 将Gravity Scale设置为0, 这样怪物就不会下坠. 此外, 在PhysicsBody2D部分下, 点击Mask属性并去除第一个复选框的勾选. 这会确保怪物不会彼此碰撞. AnimatedSprite。这一次,我们有3个动画:fly、swim、walk,每个动画在 art 文件夹中都有 2 张图片。3.Playingproperty in the Inspector to “On”. 我们将随机选择其中一个动画, 以便mobs有一些变化.AnimatedSprite2D‘sScaleproperty to(0.75, 0.75).Player场景中一样, 为碰撞添加一个CapsuleShape2D. 为了使形状与图像对齐, 您需要将Rotation Degrees(在属性检查器的 “Transorm” 下)属性设置为90. 保存该场景.敌人的脚本
Moblike this: GDScript C# C++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 <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++func _ready(): $AnimatedSprite2D.playing = true var mob_types = $AnimatedSprite2D.frames.get_animation_names() $AnimatedSprite2D.animation = mob_types[randi() % mob_types.size()]
public override void _Ready(){ var animSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D"); animSprite2D.Playing = true; string[] mobTypes = animSprite2D.Frames.GetAnimationNames(); animSprite2D.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::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()]);}
framesproperty. This returns an Array containing all three animation names:["walk", "swim", "fly"].0和2之间选取一个随机的数字, 以在列表中选择一个名称(数组索引以0起始).randi() % n会在0andn-1之中选择一个随机整数. 注解randomize(). 我们将在Main场景中使用randomize(), 因此在这里不需要添加.VisibilityNotifier2D节点的screen_exited()信号并添加以下代码: GDScript C# C++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场景. 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.
