【发布时间】:2019-12-24 17:36:15
【问题描述】:
我正在制作一个游戏,我正在尝试使用 bool shouldDie == true 来寻找敌人。 我有一个 Enemy std::list,我个人不知道代码有什么问题。 如果敌人有 shouldDie == true 我只会播放动画。 希望您能帮助我理解为什么会出现错误。
另外我也没有重载的==操作符,我在网上搜索过,不确定是否有必要……
bool foundEnemy(Enemy& enemy)
{
return enemy.shouldDie == true;
}
void Enemy::PlayDeathAnimation(std::list<Enemy>& enemies)
{
dead = true;
auto it = std::find_if(enemies.begin(), enemies.end(), foundEnemy); // where I think the error is
auto enemy = std::next(it, 0);
if (animationClock.getElapsedTime().asSeconds() >= 0.05f)
{
enemy->icon.setTextureRect(animationFrames[animationCounter]);
if (animationCounter >= 8)
{
enemies.remove(*enemy);
}
animationCounter++;
animationClock.restart();
}
}
class Enemy : public Entity
{
public:
Enemy() {}
Enemy(sf::Vector2f position, sf::Texture* texture,Player* target);
~Enemy();
void Update(sf::RenderWindow* window, float tElapsedTime);
void Draw(sf::RenderWindow* window);
void Fire(float tElapsedTime);
void CheckBullets();
void CheckEnemyBullets();
void CheckHealth();
void SetPosition(float x, float y);
bool shouldDie = false;
void PlayDeathAnimation(std::list<Enemy>& enemies);
private:
bool dead = false;
sf::Texture* deathSpriteSheet;
std::vector<sf::IntRect> animationFrames;
std::vector<Bullet> bullets;
sf::RectangleShape origin;
sf::RectangleShape aim;
Player* target;
int animationCounter = 0;
sf::Clock animationClock;
};
【问题讨论】:
-
什么是
shouldDie?你能告诉我们Enemy标头吗? -
它是 Enemy 类的公共 bool 成员
-
那么要么你向我们展示了错误的代码片段,要么你的编译器出错了,因为它抱怨
==与Enemy作为左侧操作数。 (还有专业提示,return enemy.shouldDie == true;可以是return enemy.shouldDie;。您永远不必与布尔文字进行比较) -
我再查一下
-
如果没有找到,它会返回enemy.end(),在这种情况下你的代码会有问题。