views:

58

answers:

2

Hello!

I've run into a little hurdle and wanted to see if somebody could help me!

I want to write an algorithm for an if statement that says:

if (for every 50 points)
{
//do something
}

I thought += 50 would do the trick, but nope.

Any ideas?

Thanks!

+3  A: 
if ((points % 50) == 0)
{
  // do something
}

Where points is an int variable containing your points. The if statement will be enterred for points 0, 50, 100, 150.. and so on

Thomas Joulin
This won't work if you have for example points+= 13 in the "loop"
Rippo
+1  A: 
if (points - lastCheckpoint >= 50) 
{ 
  // do something
  lastCheckpoint = points - (points % 50);
}

Start with an int lastCheckpoint = 0; during set-up, and this'll do the trick.
Caveat: If the points increase by 100 or more between checks, the // do something will only be triggered once.
If you want it to happen for every 50 points regardless, you can change the increment statement to be lastCheckpoint += 50; although this runs the risk of the points running far ahead of lastCheckpoint.

Edit: this will be more efficient:

if (points > nextCheckpoint)
{
  // do something
  nextCheckpoint = 50 + points - (points % 50);
}

Start with int nextCheckpoint = 50; This way, the test that's performed (presumably) every iteration of the game loop doesn't include a subtraction.

tzaman
Yeah. What I definitely mean is:Do something at:50, 100, 150, etc.
I still get an issue where if the player has 0 points, they'll still advance, which I don't want to happen. How do I fix that?
Did you start the `nextCheckpoint` off at 50 or 0?
tzaman