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

Author Topic: Showing the score  (Read 1448 times)

0 Members and 1 Guest are viewing this topic.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Showing the score
« on: August 11, 2013, 09:09:23 am »
Hi... So, I have a std::list of enemies spawning on the screen. I have included sf::Text in the condition where the enemies are erased from the list to show the score whenever an enemy is erased. I did the int to string conversion. It works. The score is shown as 1 for the first enemy killed. But for every consecutive enemy, it is not being incremented. What might be the mistake? Please tell me!

std::list<sf::Sprite>::iterator enemyit = enemy.begin(), next;
int erased = 0;
long double score = 0;
std::string str;
while(enemyit != enemy.end())
{
        next = enemyit;
        next++;
        if(enemyit->getGlobalBounds().intersects(player.getGlobalBounds()))
        {
                enemy.erase(enemyit);
                ++erased;
                ++score;
                str = std::to_string(score);
                text.setString(str);
        }
        enemyit = next;
}

window.draw(text);

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Showing the score
« Reply #1 on: August 11, 2013, 09:29:14 am »
long double score = 0;
Do you reset score to 0 every time ?

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Showing the score
« Reply #2 on: August 11, 2013, 09:49:40 am »
Ummm... Yeah! I thought I put it outside the while loop. It technically still was in the while(window.isOpen()) loop. I declared outside it. Now it works. Thanks, G. ;)