0

I realize that a for-loop would likely be the best way to do this, I just don't know the proper way to say for every 5 obstacles passed, add 1 to the totalScriptures integer

I have an app similar to flappy bird. An object passes between two vertical objects with the user tapping to keep it a float.

The scoring method is slightly different, so I will do my best to explain it. In the app, the user collects 'scriptures' by passing through the obstacles. For every 5 obstacles passed, the user earns 1. So in my scoring method I first divide by 2 (to account for passing between two obstacles), and then divide by 5 to figure out how many scriptures have been earned.

I am also trying to keep track of the total number of scriptures earned. I do this by creating an NSUserDefault Integer that starts at 0. What I am attempting to do is to divide by 2 to get the number of obstacles passed, then divide by 5 to get how many scriptures have been earned, and add that number to the NSUserDefault I mentioned earlier. However, with the code you are about to see, once a scripture is earned, it maintains that value, and so for the first 5 obstacles, all is well, but once one scripture is earned, it adds a scripture to the default every single time. Can someone advise me on how to only perform the addition if the finalChange number is different than the last report?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                 NSInteger startIt = [defaults integerForKey:@"totalScripturesCollected"];
                 NSInteger goBetween = _score/2;
                 NSInteger finalChange = goBetween/5;
                 NSInteger toReport = startIt + finalChange;

                 [defaults setInteger:_score/2 forKey:@"theScore"];
                 [defaults setInteger:toReport forKey:@"totalScripturesCollected"];
                 [defaults synchronize];
                 NSLog(@"startit %ld", (long)startIt);
                 NSLog(@"gobetween %ld", (long)goBetween);
                 NSLog(@"finalchange %ld", (long)finalChange);
                 NSLog(@"toreport %ld", (long)toReport);
user717452
  • 33
  • 14
  • 73
  • 149
  • How often do you call this? does _score include the scores you saved previously, or are you reseting it before each time you save? – Tiago Lira Nov 24 '14 at 17:38
  • @TiagoLira when a new game is started, _score resets to 0. It increases by 2 every time it passes between obstacles. After 5 obstacles passed, 1 scripture is earned. Goal is to increase NSUserDefault by 1 for every 5 obstacles passed. This code is called when it successfully passes through one obstacle. – user717452 Nov 24 '14 at 17:40

1 Answers1

1

I would say that you should do this calculation only once at the end of a game.

If you need to show the calculation result all the time, you can add a class variable to keep count. And in each run of that code do:

count++;
if (count == 10) {
    NSInteger toReport = startIt++;
    [defaults setInteger:toReport forKey:@"totalScripturesCollected"];
    [defaults synchronize];
    count = 0;
} 

So each 10 obstacles passed, you add one scripture, and then you start counting again.

Tiago Lira
  • 2,543
  • 1
  • 18
  • 17
  • I'll try this out, but this is being used for earning Achievements, so it kind of has to run through the app, so if they get 1 scripture it will show the achievement, and if in same level they get 10 more it will show the 10 scripture achievement. Would placing this inside that code work ok? – user717452 Nov 24 '14 at 18:01
  • Yes, use that code to update the number each time. Notice that "count" must be a saved class variable, like "_score". That should solve your problem. – Tiago Lira Nov 25 '14 at 10:59
  • Also, for future situations, my advice is that you use local and class variables for this kind of calculations, and use NSUserDefaults simply to occasionally save data (at the end of the game for example). It will make the code less confusing :) – Tiago Lira Nov 25 '14 at 11:04