SFML community forums

Help => Graphics => Topic started by: Stolle on January 09, 2015, 01:35:45 pm

Title: [solved]Collision counter, count increses 1 time per collision
Post by: Stolle on January 09, 2015, 01:35:45 pm
Hello fellas!

I'm woundering how can I make a collision counter that only increses 1 time per collision. I want to know how to get around the game loop because everytime I hit an object the counter looks like this
0
110
110
110
110
220
220
220
330
...

All the spam is ofcourse the game loop but how can I controll it to only increse one time, I've been tring to use a bool.

game.cpp
                        cout << handler.showHighScore() << endl;
                        handler.update();
                        window.draw(handler);
                        window.display();
show function
int gameHandler::showHighScore()
{
        if (this->collided == true)
        {
                this->collided = false;
                this->score.addScore();
        }
        return this->score.showHighScore();
}
When the collision is true I'm setting a bool to not increse the score any more THE COLLISION FUNCTION IS CALLED IN THE UPDATE FUNCTION
bool gameHandler::collisionCoin(Obstacle &o)
{
       
        if (player.getSprite().getGlobalBounds().intersects(o.getSprite().getGlobalBounds()))
        {
                this->show = false;
                this->collided = true;
                return true;
        }
        return false;
}

Regards Stolle
Title: Re: [solved]Collision counter, count increses 1 time per collision
Post by: Ixrec on January 09, 2015, 09:36:03 pm
Since that's nowhere near a complete and minimal code example, it's impossible to say anything for sure, but here's my guess:

Quote
When the collision is true I'm setting a bool to not increse the score any more
You seem to be implying that setting the bool to false will somehow keep collisionCoin() from changing it back to true until "this" collision is "done".  That's not how bools work.

If this comes as a surprise to you, you may wish to read a good C++ book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).

Assuming that is the problem, something like this would work better:
bool gameHandler::isCollidingWith(Obstacle &o) {
    return player.getSprite().getGlobalBounds().intersects(o.getSprite().getGlobalBounds());
}

bool gameHandler::collisionCoin(Obstacle &o) {
    if(this->isCollidingWith(o)) {
        this->show = false;
        if(!this->collisionInProgress) {
            // notice we'll only end up in here once per collision
            this->score.addScore();
        }
        this->collisionInProgress = true;
        return true;
    } else {
        this->collisionInProgress = false;
        return false;
    }
}

From a general software architecture point of view, it's also better to keep your score update logic together with the rest of your update logic.  showHighScore() is essentially a getter, so it shouldn't be taking care of updating the value as it gets it.
Title: Re: [solved]Collision counter, count increses 1 time per collision
Post by: Laurent on January 09, 2015, 10:26:32 pm
Stolle, you added [solved] to the title, could you please edit your post to make it clear that you found a solution (and possibly explain it), so that other users don't answer uselessly to your question?