Hello,I’m writing a simple shoot em up in SFML (A C++ game making framework) and I’m having trouble implementing the shooting mechanic…
In my game I declared too classes,one for enemies called “enemy”,and one for the player’s projectiles called “projectile”.
I then declared 5 objects:
enemy basic_enemy1("res/images/enemy1.png", 70, 400); //Enemy objects
enemy basic_enemy2("res/images/enemy2.png", 100, 300);
std::vector <enemy> enemies; //Vector to store enemies in
projectile pprojectile("res/images/missile.png", { 10,30 }, 1000); //Player projectile object
std::vector <projectile> pprojectiles; //vector to store projectiles in
The two enemy objects are actually declared in a separate file,along with a method that randomly chooses one of them and adds it to the “enemies” vector every 2 seconds.
Projectiles are somewhat similar,except they’re spawned when the player holds “space”,and there’s only one object so all the work is done in the “main.cpp” file.
Then I wrote a condition so test if any of the projectiles and the enemies made contact,and delete them both from their respective vectors:
for (int i = 0; i < pprojectiles.size(); i++) {
for (int j = 0; j < enemies.size(); j++) {
//This overly-complicated condition checks to see the projectile has entered the dimensions of the enemy
if (((pprojectiles[i].m_shape.getPosition().y) <= ((enemies[j].es.getPosition().y) + enemies[j].es.getSize().y / 2))
&& ((pprojectiles[i].m_shape.getPosition().x) >= ((enemies[j].es.getPosition().x) - enemies[j].es.getSize().x / 2))
&& ((pprojectiles[i].m_shape.getPosition().x) <= ((enemies[j].es.getPosition().x) + enemies[j].es.getSize().x / 2))
&& ((pprojectiles[i].m_shape.getPosition().y) >= ((enemies[j].es.getPosition().y) - enemies[j].es.getSize().y / 2))) {
pprojectiles.erase(pprojectiles.begin() + i);//If true,erase projectile/This is the problematic line
enemies.erase(enemies.begin() + j);//And erase enemy
}
}
}
Then there’s the lines that move and draw everything,with everything part of a loop that repeats every frame,no need to post those
So,for some odd reason,in certain cases,when an enemy and a projectile make contact,the game crashes,giving me the error in the title and screenshot…
Now I haven’t learnt how to debug a real-time interactive game yet,so the Visual Studio debugger can’t really help me pinpoint which line is causing this error,but after a bit of messing around with lines and values,I found it’s actually the line that erases the projectile in the condition above,I also found out that this error occurs when the enemy being hit with the projectile is relatively close to the player,like in he screenshot above.
Before,I thought it happened when there was only one projectile stored in the vector (Enemies/projectiles that go outside the screen are deleted using the same “erase();” function) but it’s inconsistent so I’m honestly not sure.
I know that this error means that the “i” index is moving outside the current range of the vector,but I don’t know why that happens,since the maximum value for the index is always limited by the current size of the vector,seen in the code above,plus,the enemy spawning/despawning process works almost identically to that of the projectiles,and it’s working just fine.
Any idea what might be causing this?
