0
\$\begingroup\$

I created a collision detection game in XNA but I have a problem with the scoring. As online tutorials says, I initialised it

score = 0;

This is in the update section to check if the person hit the object

// Check collision with person
                if (personRectangle.Intersects(numberRectangle))
                    personHit = true;

Then in the draw section..

if (personHit)
            {
               score = +score + 1;
               spriteBatch.Draw(greenBackground, mainFrame, Color.White);
            }
            else if (personHit2)
            {
                score = +score - 1;

                //device.Clear(Color.Red);
                spriteBatch.Draw(redBackground, mainFrame, Color.White);
            }

But when the person hits the objects that fall, the score goes up until that object is on the person image. I don't know if I am clear. The score doesn't go up by one per hit, it goes up until that object stops touching the person..

I was wondering how can I fix this so the score goes up by 1 only?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Try doing something like this instead:

//Person wasn't hit yet, check if a hit occured
if (!personHit && personRectangle.Intersects(numberRectangle))
{
    personHit = true;     //hit occured
    score++;              //increase score
}
else
{
    personHit = false;    //person has moved outsite hit rectangle, reset
}

You really want to keep all processing out of your draw calls. All your processing should be done in your Update portion of your game loop.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .