Hi... I am making this space game. Everything's going well so far. I managed to draw the player, the lasers and the enemies. I set the collisions such that when the laser hits the enemy, the laser and the enemy are both erased (lasers and enemy are both declared as sprite lists). The collision works... But only for the first 3 or 4 enemies. After that, the lasers don't affect the enemies. Is there anything wrong in the code? I failed to find any mistake. Here's my code:
if(!enemytex.loadFromFile("images/enemy.png"))
return EXIT_FAILURE;
std::list<sf::Sprite> enemy(MAX, sf::Sprite(enemytex));
for(std::list<sf::Sprite>::iterator enemyit = enemy.begin(); enemyit != enemy.end(); enemyit++)
{
enemyit->setOrigin(enemyit->getGlobalBounds().width/2, enemyit->getGlobalBounds().height/2);
}
std::list<sf::Sprite>::iterator enemyit = enemy.begin();
int dist = (int)(resolution.getDesktopMode().width - enemyit->getGlobalBounds().width);
int wastage = (int)enemyit->getGlobalBounds().width/2;
srand((unsigned int)time(NULL));
float y = -200;
for(std::list<sf::Sprite>::iterator enemyit = enemy.begin(); enemyit != enemy.end(); enemyit++)
{
int x = rand() % dist + wastage;
enemyit->setPosition((float)x, y);
y = y - enemyit->getGlobalBounds().height * 2;
}
if(!greenlasertex.loadFromFile("images/playerlaser.png"))
return EXIT_FAILURE;
std::list<sf::Sprite> greenlaser;
float enemyspeed = 100.f;
float playerspeed = 400.f;
float laserspeed = 800.f;
sf::Clock clock;
long double score = 0;
long double lvlnum = 1;
int shots = 0;
while(window.isOpen())
{
float timer = clock.restart().asSeconds();
window.clear();
for(std::list<sf::Sprite>::iterator enemyit = enemy.begin(); enemyit != enemy.end(); enemyit++)
{
enemyit->move(0.f, enemyspeed * timer);
window.draw(*enemyit);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
shots++;
}
if((shots != 0) && (shots % 19 == 0))
{
sf::Sprite newgreenlaser(greenlasertex);
newgreenlaser.setOrigin(newgreenlaser.getGlobalBounds().width/2, newgreenlaser.getGlobalBounds().height/2);
newgreenlaser.setScale(0.1f, 0.1f);
newgreenlaser.setPosition(player.getPosition().x, player.getPosition().y);
greenlaser.push_back(newgreenlaser);
}
for(std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(); greenlaserit != greenlaser.end(); greenlaserit++)
{
greenlaserit->move(0.f, -laserspeed * timer);
window.draw(*greenlaserit);
}
int erased = 0;
std::string str, line, word, level, lvl, number;
str = std::to_string(score);
word = "Score: ";
line = word + str;
ScoreText.setString(line);
number = std::to_string(lvlnum);
lvl = "Level ";
level = lvl + number;
LevelText.setString(level);
***INSIDE THIS BOX***
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::list<sf::Sprite>::iterator enemyit = enemy.begin(), next;
std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(), reload;
while(enemyit != enemy.end())
{
next = enemyit;
next++;
while(greenlaserit != greenlaser.end())
{
reload = greenlaserit;
reload++;
if(enemyit->getGlobalBounds().intersects(greenlaserit->getGlobalBounds()))
{
enemy.erase(enemyit);
greenlaser.erase(greenlaserit);
++erased;
score = score + 10;
int diff = (int)score;
if(diff % 500 == 0)
{
enemyspeed = enemyspeed + 25.f;
lvlnum++;
number = std::to_string(lvlnum);
level = lvl + number;
LevelText.setString(level);
}
str = std::to_string(score);
line = word + str;
ScoreText.setString(line);
}
greenlaserit = reload;
}
enemyit = next;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
***INSIDE THIS BOX***
for(int i = 0; i < erased; ++i)
{
sf::Sprite temp(enemytex);
temp.setOrigin(temp.getGlobalBounds().width/2, temp.getGlobalBounds().height/2);
float y = -200;
int x = rand() % dist + wastage;
temp.setPosition((float)x, y);
y = y - temp.getGlobalBounds().height * 2;
enemy.push_back(temp);
}
window.display();
}
I apologize if the code is too long. That is as minimal and complete as it can get. I wanted to show the whole code of the enemy and laser because I can't pinpoint the problem, and it is pretty big. Nevertheless, I highlighted the collision part inside the code for easier reference. Please help. Thank you.