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

Author Topic: [solved]Collision counter, count increses 1 time per collision  (Read 1795 times)

0 Members and 1 Guest are viewing this topic.

Stolle

  • Newbie
  • *
  • Posts: 6
    • View Profile
[solved]Collision counter, count increses 1 time per collision
« 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
« Last Edit: January 09, 2015, 02:07:28 pm by Stolle »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: [solved]Collision counter, count increses 1 time per collision
« Reply #1 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.

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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [solved]Collision counter, count increses 1 time per collision
« Reply #2 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?
Laurent Gomila - SFML developer

 

anything