Reputation: 26971
I am using this code to see if a users score is divisible by 20.
if(hitCount % 20 == 0){
interval = interval - 1;
Log.e("Score", "User score is divisible by 20 decreasing interval to "
+ interval);
timer.setInterval(interval);
}
The only problem is is that this code is in a IUpdate method in andengine that updates every second.
So if a user score is divisible by 20 for more than 1 second it keeps subtracting 1.
I only want it to subtract once if the user score is divisible by 20.
Any suggestions?
Upvotes: 1
Views: 111
Reputation: 4463
I know there is a selected answer, but:
Rather than checking if the score is divisible by 20 in the update thread, why don't you check it when you update the score? It will reduce the number of times that you check for '% 20' (increasing efficiency) and will also solve your issue.
Upvotes: 2
Reputation: 224904
You could keep an external flag:
bool alreadySet = false;
Then, in your code, set or unset it:
if(hitCount % 20 == 0) {
if(!alreadySet) {
alreadySet = true;
// Continue with your code
}
} else {
alreadySet = false;
}
I see what you mean though - it does feel like a bit of a messy solution.
Upvotes: 3
Reputation: 18492
You can use a flag to check if hitCount
has already been divisible by 20:
boolean bDivBy20 = false; // outside of the method
...
if(!bDivBy20 && hitCount % 20 == 0){
bDivBy20 = true;
interval = interval - 1;
Log.e("Score", "User score is divisible by 20 decreasing interval to " + interval);
timer.setInterval(interval);
}
Also, if you want to enter the if
block, but only execute the line interval = interval - 1;
once, then move the check inside the outer if
block, eg:
if(hitCount % 20 == 0){
...
if (!bDivBy20) {
...
}
...
}
Upvotes: -2