Hi i wrote this code for creating multiple enemies from enemy class
int number = 10;
std::vector<std::unique_ptr<enemy> > enemies1;
for (unsigned i = 0; i < number; ++i) {
enemies1.emplace_back(std::unique_ptr<enemy>(new enemy(sf::Vector2f(100 * i + 1500, 1000), sf::Vector2f(15, 10), sf::Color(255, 255, 255, 0))));
}
//update
for (unsigned i = 0; i < number; ++i) {
enemies1[i]->update();
}
//render
for (unsigned i = 0; i < number; ++i) {
window.draw(enemies1[i]->enemysprite);
}
everything is ok. Multiple Sprites are drawing on screen and moving and such. But my problem is i cant erase a single enemy from enemies1 vector. I learnt this code from a site and there is a sample code like this:
if (!items1.empty())
enemies1.erase(std::remove_if(items1.begin(), enemies1.end(), [sprite](const std::unique_ptr <enemy> &e) { return e->rect.getGlobalBounds().intersects(sprite.getGlobalBounds()); }), enemies1.end());
but it doesnt work and program is stopping to work. Basically i tried this code for another array like this:
std::vector<int> vect_array(10);
std::vector<int>::iterator it = vect_array.begin() + 2;-
vect_array.insert(it, 66);//add
vect_array.push_back(21);
vect_array.erase(std::remove(vect_array.begin(), vect_array.end(), 5), vect_array.end());
and it works.
My question is what's wrong on top code? it works with numbers but i want to do same things with another array for spawn and delete sprites.
I appreciate for every advice.