如何修复'Expression:向量下标超出范围'错误

时间:2019-08-08 16:08:24

标签: c++ visual-studio sfml

我正在通过SFML制作简单的游戏,并且遇到了以下错误:

调试断言失败! 表达式:向量下标超出范围

仅当“火箭”与第一个“敌人”接触而屏幕上同时有3个敌人时,才会出现此错误。

    for (int i = 0; i < rockets.size(); i++) //This is where the error points to
    {
        for (int j = 0; j < enemies.size(); j++)
        {
            Rocket *rocket = rockets[i];
            Enemy *enemy = enemies[j];

            if (checkCollision(rocket->getSprite(), enemy->getSprite()))
            {
                //hitSound.play();
                score++;
                std::string finalScore = "Score: " + std::to_string(score);
                scoreText.setString(finalScore);
                sf::FloatRect scoreBounds = scoreText.getGlobalBounds();
                scoreText.setOrigin(scoreBounds.width / 2, scoreBounds.height / 2);
                scoreText.setPosition(viewSize.x * 0.5f, viewSize.y * 0.10f);



                rockets.erase(rockets.begin() + i);
                enemies.erase(enemies.begin() + j);

                delete(rocket);
                delete(enemy);
                if (score % 5 == 0) levelUp();
                printf("rocket intersects with enemy \n");
            }
        }
    }

我已经将敌人和火箭物体存储在矢量中:

    std::vector <Enemy*> enemies;
    std::vector <Rocket*> rockets;

如果有帮助,这是我的整个更新功能:

void update(float dt)
{
    hero.update(dt);

    currentTime += dt;
    if (currentTime >= prevTime + 1.125f)
    {
        spawnEnemy();
        prevTime = currentTime;
    }

    for (int i = 0; i < enemies.size(); i++)
    {
        Enemy *enemy = enemies[i];
        enemy->update(dt);

        if (enemy->getSprite().getPosition().x < 0)
        {
            enemies.erase(enemies.begin() + i);
            delete(enemy);
            gameOver = true;
            gameOverSound.play();
        }
    }

    for (int i = 0; i < rockets.size(); i++)
    {
        Rocket *rocket = rockets[i];

        rocket->update(dt);

        if (rocket->getSprite().getPosition().x > viewSize.x)
        {
            rockets.erase(rockets.begin() + i);
            delete(rocket);
        }
    }

    for (int i = 0; i < rockets.size(); i++)
    {
        for (int j = 0; j < enemies.size(); j++)
        {
            Rocket *rocket = rockets[i];
            Enemy *enemy = enemies[j];

            if (checkCollision(rocket->getSprite(), enemy->getSprite()))
            {
            //hitSound.play();
                score++;
                std::string finalScore = "Score: " + std::to_string(score);
                scoreText.setString(finalScore);
                sf::FloatRect scoreBounds = scoreText.getGlobalBounds();
                scoreText.setOrigin(scoreBounds.width / 2, scoreBounds.height / 2);
                scoreText.setPosition(viewSize.x * 0.5f, viewSize.y * 0.10f);



                rockets.erase(rockets.begin() + i);
                enemies.erase(enemies.begin() + j);

                delete(rocket);
                delete(enemy);
                if (score % 5 == 0) levelUp();
                printf("rocket intersects with enemy \n");
            }
        }
    }

    for (int i = 0; i < enemies.size(); i++)
    {
        Enemy *enemy = enemies[i];

        if (checkCollision(enemy->getSprite(), hero.getSprite()))
        {
            gameOver = true;
            gameOverSound.play();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在遍历向量时不能将其从向量中删除。您正在删除元素,但未正确调整i和j,因此最终超出了索引范围。