从 Vector 访问派生类中的对象

发布于 2024-10-11 19:42:22 字数 1092 浏览 6 评论 0原文

我有一个 Entity 基类,类 Player 和 Enemy 继承它。

class Entity
{
  public:

    virtual void Update(sf::RenderWindow &window) {};
    virtual void Draw(sf::RenderWindow &window) {};

};

玩家和敌人都包含一个如下所示的精灵对象:

class Player : Entity
{
   public:

   sf::Sprite sprite

    void Update(sf::RenderWindow &window);
    void Draw(sf::RenderWindow &window)
}

玩家和敌人是在一个向量内创建的,该向量的设置如下:

class EntityManager
{
   public:
   void CollisionCheck();
   private:
   std::vector<Entity*> entityVector;
}

我正在寻找使用这种形式的碰撞检测功能:

bool Collision::CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2)

所以我正在尝试做一些事情像这样:

void EntityManager::ColCheck()
{
   if (Collision::CircleTest(entityVector[0]->sprite, entityVector[1]->sprite))
      {
         cout << "COLLISION\n";
      }
}

这会导致以下编译错误:

“类实体”没有名为“精灵”的成员

我不确定如何在实体中创建虚拟精灵,以便我可以使用上述方法访问玩家和敌人精灵。这可能吗?

我很困惑,非常感谢任何人可以提供的帮助!

I have an Entity baseclass which the classes Player and Enemy Inherit.

class Entity
{
  public:

    virtual void Update(sf::RenderWindow &window) {};
    virtual void Draw(sf::RenderWindow &window) {};

};

Both player and enemy contain a sprite object that looks like this:

class Player : Entity
{
   public:

   sf::Sprite sprite

    void Update(sf::RenderWindow &window);
    void Draw(sf::RenderWindow &window)
}

Player and Enemy are created inside a vector which is set up like this:

class EntityManager
{
   public:
   void CollisionCheck();
   private:
   std::vector<Entity*> entityVector;
}

I'm looking to use a collision detection function of this form:

bool Collision::CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2)

So I'm trying to do something like this:

void EntityManager::ColCheck()
{
   if (Collision::CircleTest(entityVector[0]->sprite, entityVector[1]->sprite))
      {
         cout << "COLLISION\n";
      }
}

This results in the following compile error:

‘class Entity’ has no member named ‘sprite’

I'm unsure how to create a dummy sprite in Entity so that I can access the player and enemy sprites using the above method. Is this possible?

I'm stumped and would greatly appreciate any help anyone can offer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韶华倾负 2024-10-18 19:42:22

如果代码中从 Entity 派生的所有内容都有 sprite 对象,那么您应该在基类中声明该对象。

不在基类中声明该对象意味着可能存在一个继承自 Entity 的类,该类没有 sprite 对象,这意味着 ColCheck 没有有效的假设基础entityVector 的元素指向具有名为 sprite 的变量的对象。有道理吗?

If everything in your code that derives from Entity has a sprite object, then you should declare that object in the base class.

Not declaring the object in the base class means that there could be a class inheriting from Entity that does not have a sprite object, which means that ColCheck has no valid basis for assuming that elements of entityVector point to something that has a variable called sprite. Make sense?

水晶透心 2024-10-18 19:42:22

您可能不应该创建虚拟对象,除非所有实体都拥有精灵。

您可能想要的是使用访问者模式或可能是许多多重分派实现之一。哪个和什么最终取决于你。

You probably shouldn't create a dummy unless having a sprite is something ALL entities have.

What you might want is to use a visitor pattern or possibly one of the many multiple-dispatch implementations. Which and what will end up having to be up to you.

橘虞初梦 2024-10-18 19:42:22

如果 PlayerEnemy 类都包含 sprite,为什么不在 Entity 中声明它呢?这应该可以解决你的问题。

If both Player and Enemy classes contain sprite, why not declare it inside Entity? This should solve your problem.

千年*琉璃梦 2024-10-18 19:42:22

您可以创建一个成员函数 sprite(),在 Entity 中声明为纯虚函数:

class Entity {
public:
    virtual void Update(sf::RenderWindow &window) {};
    virtual void Draw(sf::RenderWindow &window) {};

    virtual sf::Sprite& sprite() = 0;
};

然后,PlayerEnemy 实现将返回每个都有的 sf::Sprite 实例变量。然而,其他海报提出了一个有效的观点;如果所有派生类都将拥有精灵,那么将精灵放在基类中可能是有意义的。

You could make a member function sprite() that is declared as a pure virtual function inside Entity:

class Entity {
public:
    virtual void Update(sf::RenderWindow &window) {};
    virtual void Draw(sf::RenderWindow &window) {};

    virtual sf::Sprite& sprite() = 0;
};

Then, the Player and Enemy implementations would return the sf::Sprite instance variables each has. However, the other posters bring up a valid point; it might make sense to have the sprite be in the base class if all derived classes are going to have one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文