If I shoot a bullet and then another and the first bullet hits an enemy the program crashes.
If I shoot a bullet and hits an enemy but I dont shoot a 2nd one its fine.
If I shoot a bullet, it hits the enemy and then shoot a 2nd one its ok.
If I shoot any ammount of bullet and neither shooot the enemy its ok.
If I shoot a bullet that doesent it the enemy and another that hits while the first is active its ok.
The code for drawing the bullets:
void drawBullet(sf::RenderWindow& game, float playerRotation, float xPlayer, float yPlayer, sf::Vector2f vectorPosition) {
float time2;
time = clock.getElapsedTime();
//std::cout << std::fixed << time.asSeconds() << std::endl;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && time.asSeconds() > 0.5 && cateGloante > 0) {
cateGloante--;
bullet.setRotation(playerRotation);
bullet.setPosition(vectorPosition);
rota = playerRotation;
vectorOfRotations.push_back(rota);
clock.restart();
vectorOfBullets.push_back(bullet);
}
for (int i = 0; i < vectorOfBullets.size(); i++) {
game.draw(vectorOfBullets[i]);
vectorOfBullets[i].move(cos(vectorOfRotations[i] * 3.14189 / 180) * 3, sin(vectorOfRotations[i] * 3.14189 / 180) * 3);
}
}
The code in the main.cpp
for (int i = 0; i < enemy1.vectorOfEnemies.size(); i++) {
for (int j = 0; j < bullet1.vectorOfBullets.size(); j++) {
if (enemy1.vectorOfEnemies[i].getGlobalBounds().intersects(bullet1.vectorOfBullets[j].getGlobalBounds())) {
bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + j);
bullet1.vectorOfRotations.erase(bullet1.vectorOfRotations.begin() + j);
enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
bullet1.cateGloante++;
}
}
}
And the drawEnemies code
void drawEnemies(sf::RenderWindow& game) {
srand(time(0));
if (randomized == false) {
enemy.setPosition(1 + (rand() % 1920), 1 + (rand() % 1080));
randomized = true;
}
enemy.setOrigin(25, 62.5);
if(oSinguraData == true) vectorOfEnemies.push_back(enemy);
oSinguraData = false;
for (int i = 0; i < vectorOfEnemies.size(); i++) {
game.draw(vectorOfEnemies[i]);
}
}
I do not know what doesent work while the 2 bullets are active...