Ok, This all looks completly correct to me, but it's working sometimes, and not working sometimes. Is there a better way of doing this or am I missing something? Just trying to save High score to NSUserDefaults!
-(void)updateScore
{
NSString * currentScore = _scoreLabel.string;
NSLog(#"Current Score, %#", _scoreLabel.string);
NSString * newHighScore = _highScoreLabel.string;
NSLog(#"High Score, %#", _highScoreLabel.string);
if (currentScore > newHighScore) {
NSLog(#"updating high score!");
_highScoreLabel.string = currentScore;
NSUserDefaults *scoreDefaults = [NSUserDefaults standardUserDefaults];
[scoreDefaults setObject:_highScoreLabel.string forKey:#"high_Score"];
[scoreDefaults synchronize];
}
// string for key.
NSLog(#"Saved High Score, %#",[[NSUserDefaults standardUserDefaults] stringForKey:#"high_Score"]);
}
Here is the log output.
// Worked ok here.
2014-04-04 13:58:46.921 [2378:60b] game over
2014-04-04 13:58:46.923 [2378:60b] Current Score, 5
2014-04-04 13:58:46.924 [2378:60b] High Score, 4
2014-04-04 13:58:46.925 [2378:60b] updating high score!
2014-04-04 13:58:46.929 [2378:60b] Saved High Score, 5
// Worked ok here. Did not update score because it was less than high score.
2014-04-04 13:59:01.321 [2378:60b] game over
2014-04-04 13:59:01.323 [2378:60b] Current Score, 1
2014-04-04 13:59:01.325 [2378:60b] High Score, 5
2014-04-04 13:59:01.326 [2378:60b] Saved High Score, 5
// Did NOT WORK, current score was higher than saved high score.
2014-04-04 13:59:22.087 [2378:60b] game over
2014-04-04 13:59:22.089 [2378:60b] Current Score, 6
2014-04-04 13:59:22.092 [2378:60b] High Score, 5
2014-04-04 13:59:22.093 [2378:60b] Saved High Score, 5
The problem is unrelated to NSUserDefaults.
if (currentScore > newHighScore) ...
compares the pointers to the Objective-C objects.
You want to compare the integer values:
if ([currentScore integerValue] > [newHighScore integerValue]) ...
Related
I have code in place for the scoring in a jumper game that I am creating. The score increases as the player moves up the screen. This code below works fine for keeping track of the score but the resulting Int is very high. Thus, a high score would result in the millions. I am trying to figure out how to divide the scoring increments, so that a high score would result in the thousands instead of the millions.
In line two, I tried putting / 2 after after ...position.y, but that resulted in an extremely low negative Int.
Any suggestions?
if Int(player.position.y) > maxPlayerY! {
GameState.sharedInstance.score += Int(player.position.y) - maxPlayerY!
maxPlayerY = Int(player.position.y)
scoreLabel.text = String(format: "Score: %d", GameState.sharedInstance.score)
}
how to post/Submit my time in game center in iOS acutely i am complete my game with in 2 mint and Submit my time game center
02.00
and acutely leaderboard save time like this 0.00.02
i want submit my time look like in leaderboard 0.02.00
currentScore=02.22;
[[GameCenterManager sharedManager] saveAndReportScore:currentScore leaderboard:leaderboardNameID
sortOrder:GameCenterSortOrderLowToHigh];
The score parameter is an integer, so you should be tracking the number of seconds as the score, not the number of minutes:
int score = 2 * 60 + 22; // 142 seconds = 2m22s
Reference
I'm trying to get the rotation of the device in rubymotion. I added this piece of code in the viewDidLoad.
#motionManager = CMMotionManager.alloc.init
#motionManager.deviceMotionUpdateInterval = 1.0
if (#motionManager.isDeviceMotionAvailable)
queue = NSOperationQueue.currentQueue
#motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler:lambda do |motion, error|
NSLog "error = %#", error
NSLog "rotation rate = [%f, %f, %f]", motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z
end)
else
NSLog "Device Motion is not available: You're likely running this in a simulator"
end
But it always logs this:
rotation rate = [0.000000, 0.000000, 0.000000]
What is wrong with what I do?
In RubyMotion, floats are actually objects, so you need to use %# instead:
NSLog "rotation rate = [%#, %#, %#]", motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z
If you wanted to format the numbers (e.g. to 3 decimal places), you would need to format the arguments separately:
NSLog "rotation rate = [%#, %#, %#]", "%.3f" % motion.rotationRate.x, "%.3f" % motion.rotationRate.y, "%.3f" % motion.rotationRate.z
See this twitter conversation with one of the RubyMotion developers.
I'd like to show a player's high score in context with other players' high scores. In other words, I want to create a list that shows where the player stands compared to the competition.
The list might look something like this:
1st: 1,000,000
...
436th: 125,285
437th: 124,132 (your score)
438th: 120,998
439th: 119,212
...
1012th: 1,433
This example shows the global top and bottom scores, as well as scores neighboring the player's personal best.
Is there any way to retrieve such a list using GameKit?
EDIT/UPDATE: I slightly reworded this question and posted it to the Apple developer forums here.
Well, AFAIK there's no such way to do it in one request, but GC returns your own score in every scores request, so you can first request any (e.g. first) row in leader board, determine your own position and then create new request with positions from: your_own-desired_range to: your_own+desired_range.
_leaderboard.category = kLeaderboardID;
_leaderboard.timeScope = GKLeaderboardTimeScopeAllTime;
_leaderboard.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
_leaderboard.range = NSMakeRange(1, 1);
[_leaderboard loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error){
//processing, checking errors, etc
_leaderboard.range = NSMakeRange([_leaderboard.localPlayerScore rank] - 4, 8);
[_leaderboard loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error){
// Here are your results
}
}
I am trying to submit a float of two decimal length to my Game Center leaderboard, however the only format allowed to submit with is int64_t. I am using the default Apple report score method:
- (void)reportScore:(int64_t)score forCategory:(NSString *)category {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler: ^(NSError *error) {
[self callDelegateOnMainThread: #selector(scoreReported:) withArg: NULL error: error];
}];
}
I am trying to use this method to provide the score to the report score method:
- (IBAction)increaseScore {
self.currentScore = self.currentScore + 1;
currentScoreLabel.text = [NSString stringWithFormat: #"%lld", self.currentScore];
NSLog(#"%lld", self.currentScore);
}
Please help, I have been googling like crazy and cannot find the answer to this.
GameCenter only accepts int64_t
The only difference between values that appear like floats or decimal values and those that appear as integers is the position of the decimal mark, while in fact all of them are int64_t.
If your internal representation is a double and you configured game center to show 3 digits after the decimal mark you have to convert it to an integer by multiplying with 10^3 and casting to integer.
int64_t gameCenterScore = (int64_t)(doubleValue * 1000.0f)
You can only submit 64 bit integers as scores to a leaderboard. From the documentation:
To Game Center, a score is just a
64-bit integer value reported by your
application. You are free to decide
what a score means, and how your
application calculates it. When you
are ready to add the leaderboard to
your application, you configure
leaderboards on iTunes Connect to tell
Game Center how a score should be
formatted and displayed to the player.
Further, you provide localized strings
so that the scores can be displayed
correctly in different languages. A
key advantage of configuring
leaderboards in iTunes Connect is that
the Game Center application can show
your game’s scores without you having
to write any code.
That doc page should tell you about formatting your score. It sounds like in order to display float-like scores you will have to tinker with the format settings in iTunes Connect.
Update
Try this for increaseScore:
- (IBAction) increaseScore {
self.currentScore = self.currentScore + 5;
float score = (float)self.currentScore / 100.0f;
currentScoreLabel.text = [NSString stringWithFormat: #"%f", score];
NSLog(#"%lld", self.currentScore);
}
You can see the GKScore.h file.
#property(nonatomic, assign) int64_t value; // The score value as a 64bit integer.
So float value now is not available.