Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I want to destroy an object from the screen and it crashes  (Read 2803 times)

0 Members and 1 Guest are viewing this topic.

AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
I want to destroy an object from the screen and it crashes
« on: April 12, 2021, 08:12:22 pm »
I made a vector, vectorOfEnemies and vectorOfBullets witch I want to destroy the object when they intersect on the map(sry for the bad english).

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())) {
                    /*enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
                    for (int k = i; k < enemy1.vectorOfEnemies.size() - 1; k++) {
                        enemy1.vectorOfEnemies[k] = enemy1.vectorOfEnemies[k + 1];
                    } enemy1.vectorOfEnemies.pop_back();
                    bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + i);
                    for (int l = i; l < bullet1.vectorOfBullets.size() - 1; l++) {
                        bullet1.vectorOfBullets[l] = bullet1.vectorOfBullets[l + 1];
                    } bullet1.vectorOfBullets.pop_back();
                    for (int p = i; p < bullet1.vectorOfRotations.size(); p++) {
                        bullet1.vectorOfRotations[p] = bullet1.vectorOfRotations[p + 1];
                    } bullet1.vectorOfRotations.pop_back();*/

                   
                   
                    bullet1.cateGloante++;
                    //std::cout << bullet1.cateGloante;
                    game.clear();
                    game.display();
                }
            }
        }


that is the code I tried to do to destroy the objects and when I run the program when the objects intersect the program crashes with the error "Expression: vecot subscript out of range"

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: I want to destroy an object from the screen and it crashes
« Reply #1 on: April 13, 2021, 03:20:59 pm »
Essentially a duplicated of this and this

Please stick to a single thread for the same topic, so we don't risk repeating ourselves.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything