从 Vector 访问派生类中的对象
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果代码中从
Entity
派生的所有内容都有sprite
对象,那么您应该在基类中声明该对象。不在基类中声明该对象意味着可能存在一个继承自
Entity
的类,该类没有 sprite 对象,这意味着ColCheck
没有有效的假设基础entityVector
的元素指向具有名为sprite
的变量的对象。有道理吗?If everything in your code that derives from
Entity
has asprite
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 thatColCheck
has no valid basis for assuming that elements ofentityVector
point to something that has a variable calledsprite
. Make sense?您可能不应该创建虚拟对象,除非所有实体都拥有精灵。
您可能想要的是使用访问者模式或可能是许多多重分派实现之一。哪个和什么最终取决于你。
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.
如果
Player
和Enemy
类都包含sprite
,为什么不在Entity
中声明它呢?这应该可以解决你的问题。If both
Player
andEnemy
classes containsprite
, why not declare it insideEntity
? This should solve your problem.您可以创建一个成员函数
sprite()
,在Entity
中声明为纯虚函数:然后,
Player
和Enemy
实现将返回每个都有的sf::Sprite
实例变量。然而,其他海报提出了一个有效的观点;如果所有派生类都将拥有精灵,那么将精灵放在基类中可能是有意义的。You could make a member function
sprite()
that is declared as a pure virtual function insideEntity
:Then, the
Player
andEnemy
implementations would return thesf::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.