C++从向量中的类返回精灵对象
我有一个 Entity 基类,类 Player 和 Enemy 继承它。玩家和敌人都包含一个精灵对象(来自 SFML api),如下所示:
class Player : Entity
{
sf::Sprite sprite
}
玩家和敌人是在向量内创建的,该向量的设置如下:
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";
}
}
但我收到此编译错误:
错误:请求成员“精灵”
'((EntityManager*)this)->EntityManager::entityVector.std::vector<_Tp, _Alloc>::operator[],其中 _Tp = Entity*,_Alloc = std::allocator',其中 属于非类类型“Entity*”
如何将精灵对象从向量内的这些类传递到碰撞函数?
I have an Entity baseclass which the classes Player and Enemy Inherit. Both player and enemy contain a sprite object (From the SFML api) that looks like this:
class Player : Entity
{
sf::Sprite sprite
}
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 which is 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";
}
}
But I get this compile error:
error: request for member ‘sprite’ in
‘((EntityManager*)this)->EntityManager::entityVector.std::vector<_Tp,
_Alloc>::operator[] with _Tp = Entity*, _Alloc =
std::allocator’, which
is of non-class type ‘Entity*’
How do I pass the sprite object from these classes inside the vector to the collision function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
entityVector
包含Entity*
,因此您需要使用正确的语法:entityVector[0]->sprite
而不是entityVector [0].精灵
。Since
entityVector
holdsEntity*
you'll need to use the proper syntax:entityVector[0]->sprite
instead ofentityVector[0].sprite
.