SFML community forums

Help => General => Topic started by: WDR on August 11, 2013, 09:09:23 am

Title: Showing the score
Post by: WDR 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);
Title: Re: Showing the score
Post by: G. on August 11, 2013, 09:29:14 am
long double score = 0;
Do you reset score to 0 every time ?
Title: Re: Showing the score
Post by: WDR 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. ;)