Why is this highscore glitching? - ios

For some reason, the highscore on the game over page is not properly displayed. After a round of play, both the score and highscore are displayed on the game over menu. The score is displayed fine, but in the event that the user beats their highscore, it doesn't update, until the menu is reloaded.
If thats confusing:
Game 1: score = 30, previous highscore displayed = 10
Game 2: score = 40, previous highscore displayed = 30
Game 3: score = 10, highscore displayed = 30
So essentially it doesn't display the new highscore when it is achieved, until the next time the game over menu is loaded.
This is the code:
-(void)update:(CFTimeInterval)currentTime {
score = _debris.count + _debris2.count;
_scoreLabel.text = [NSString stringWithFormat:#"%d", score];
}
and in the initWithSize method:
highScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"highScoreSaved"];
if (score > highScoreNumber) {
highScoreNumber = score;
[[NSUserDefaults standardUserDefaults] setInteger:highScoreNumber forKey:#"highScoreSaved"];
That's the code in the game, and this is the code on the game over menu:
SKLabelNode *highScore = [SKLabelNode labelNodeWithFontNamed:#"DIN Condensed"];
highScore.fontSize = 40;
highScore.alpha = 0.7;
highScore.fontColor = [SKColor whiteColor];
highScore.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-15);
highScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"highScoreSaved"];
highScore.text = [NSString stringWithFormat:#"BEST: %d", highScoreNumber];
Based on this, is there any reason as to why the highscore isn't updating when there is a new highscore, but instead waiting until the gamoever screen is reloaded?

Are you calling [[NSUserDefaults standardUserDefaults] synchronize] right after [[NSUserDefaults standardUserDefaults] setInteger:highScoreNumber forKey:#"highScoreSaved"];? That will ensure the data is saved immediately.

Related

Score label node disappears after first round

I don't know why, but score label node dissapears after first round.I have two scores, one for human ,other for computer :
-(void)scoreCount{
if(scoreLabel == nil){
NSString* scoretxt =[NSString stringWithFormat:#"0"];
[scoreLabel setText:scoretxt];
scoreLabel = [SKLabelNode labelNodeWithFontNamed:#"ROTORcapExtendedBold"];
scoreLabel.fontSize = 65.f;
scoreLabel.fontColor = [UIColor grayColor];
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame)/2,CGRectGetMaxY(self.frame)-70);
scoreLabel.zPosition = -1;
[self addChild:scoreLabel];
}
scoreLabel.text = [NSString stringWithFormat:#"%ld",(long)score];
if(scoreLabelCom == nil){
NSString* scoretxtcom =[NSString stringWithFormat:#"0"];
[scoreLabelCom setText:scoretxtcom];
scoreLabelCom = [SKLabelNode labelNodeWithFontNamed:#"ROTORcapExtendedBold"];
scoreLabelCom.fontSize = 65.f;
scoreLabelCom.fontColor = [UIColor grayColor];
scoreLabelCom.position = CGPointMake(CGRectGetMidX(self.frame)+(CGRectGetMidX(self.frame)/2),CGRectGetMaxY(self.frame)-70);
scoreLabelCom.zPosition = -1;
[self addChild:scoreLabelCom];
}
scoreLabelCom.text = [NSString stringWithFormat:#"%ld",(long)scoreCom];
}
this method is called every time somebody is getting a point, and I put in
-(void)update:(CFTimeInterval)currentTime {
[self scoreCount];
}
because, without it scoreCount wont show 0 points, but only show up after first point, but, when new round starts, ScoreCout wont show up at all.
how can I correct it? And why it is happening?
This (long)score and (long)scoreCom
You can add value to it by now.
score = score + 1; //Before add to nsstring
scoreLabel.text = [NSString stringWithFormat:#"%ld",(long)score];
And
scoreCom = scoreCom + 1;//Before add to nsstring
scoreLabelCom.text = [NSString stringWithFormat:#"%ld",(long)scoreCom];
Well, I don't know how good it is but I add scoreLabel = nil and scoreLabelCom = nil at didBeginContactwhen game ends, and it works now.

Highscore Popup If New

I want to implement a simple concept. If the player gets a new highscore, there will be a sprite newHS that will become visible, otherwise it is hidden. Here is my code:
if (score < highScore) {
newHS.visible = NO;
}
else {
highScore = score;
[[NSUserDefaults standardUserDefaults] setInteger:highScore forKey:#"HighScore"];
[[NSUserDefaults standardUserDefaults]synchronize];
newHS.visible = YES;
}
However, I ran into a problem. If I start off by scoring 1, it pops up just fine. Then if I score 1 again it still pops up (because score is not less than highScore, it's the same), but I don’t want that because it’s not a NEW highscore anymore.. How would I get around this? I thought of adding another variable, something like “previousScore” or something but I am not sure how to approach this..
int highscore = [[NSUserDefaults standardUserDefaults] valueForKey:#"HighScore"];
if (score > highscore) {
newHS.visible = YES;
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else newHS.visible = NO;
You don't show us how or where you create the highscore integer, my guess is that you create a new local highscore int and set it to 0 instead of set it to the saved highscore.

iOS - Retrieving and saving scores of a game

I developed a small game , , so I manage something like this , when app lunches for the first time , it save the very first record , then after that it loads and compares the new records with the the first time record . but the problem is when user hit the record it should be saved the new record but it doesn't !!! and saves the first record again ! here is my code :
- (void)manageScores {
NSLog(#"managed");
NSString *tempScore = [NSString stringWithFormat:#"%#",scoreLabel.text];
GO.scoreNumber = [tempScore integerValue];
GO.bestScoreNumber = [tempScore integerValue];
GO.scores.text = [NSString stringWithFormat:#"score:%d",GO.scoreNumber];
GO.bestScore.text = [NSString stringWithFormat:#"best:%d",GO.bestScoreNumber];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
// app already launched
NSLog(#"app already launched");
[GO loadBestScore];
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
// This is the first launch ever
NSLog(#"first time lunch ever");
[GO saveBestScore];
}
if (GO.scoreNumber > GO.bestScoreNumber) {
//**************THIS METHOD DOESN'T WORK WHEN USER HIT THE RECORD*********/////
[GO saveBestScore];
//**************//
GO.scores.text = [NSString stringWithFormat:#"score:%d",GO.scoreNumber];
GO.bestScore.text = [NSString stringWithFormat:#"best:%d",GO.scoreNumber];
GO.shareScore.text = [NSString stringWithFormat:#"score:%d",GO.bestScoreNumber];
NSLog(#"new record");
}
if (GO.scoreNumber < GO.bestScoreNumber) {
GO.scores.text = [NSString stringWithFormat:#"score:%d",GO.scoreNumber];
GO.bestScore.text = [NSString stringWithFormat:#"best:%d",GO.bestScoreNumber];
GO.shareScore.text = [NSString stringWithFormat:#"score:%d",GO.bestScoreNumber];
[GO loadBestScore];
NSLog(#"NO NEW RECORD");
}
}
saving and loading scores (methods form GameOver.h/m (GO) )
- (void)saveBestScore {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:bestScoreNumber forKey:#"highScore"];
[defaults synchronize];
NSLog(#"BEST SCORE SAVED:%i",bestScoreNumber);
}
- (void)loadBestScore {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
bestScoreNumber = (int)[prefs integerForKey:#"highScore"];
}
Looks like you should save scoreNumber rather than bestScoreNumber
Reason:
The following code wants to save the best score because GO.ScoreNumber > Go.BestScoreNumber.
if (GO.scoreNumber > GO.bestScoreNumber) {
//**************THIS METHOD DOESN'T WORK WHEN USER HIT THE RECORD*********/////
[GO saveBestScore];
//**************//
GO.scores.text = [NSString stringWithFormat:#"score:%d",GO.scoreNumber];
GO.bestScore.text = [NSString stringWithFormat:#"best:%d",GO.scoreNumber];
GO.shareScore.text = [NSString stringWithFormat:#"score:%d",GO.bestScoreNumber];
NSLog(#"new record");
}
But in saveBestScore, it stores bestScoreNumber, which is the previous highest score number.
[defaults setInteger:bestScoreNumber forKey:#"highScore"];
- (void)saveBestScore
This method doesn't take an argument, but it should.
If you're keeping scores as int, you should modify it to look like this:
- (void)saveBestScore:(int)bestScore
Then, on first launch, call saveBestScore with an argument of 0, just to get it initialize. In fact, if you write saveBestScore method correctly, you don't really need to check whether the app has already launched ever.
- (void)saveBestScore:(int)bestScore {
if (bestScore > [[[NSUserDefaults standardUserDefaults]
objectForKey:#"BestScore"] intValue]); {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber
numberWithInt:bestScore] forKey:#"BestScore"];
}
- (int)loadBestScore {
return [[[NSUserDefaults standardUserDefaults] objectForKey:#"BestScore]
intValue];
}

How to save and call Highscore in Cocos2D

How to save my games highscore in cocos2d, i already have a variable called score which displays the score while playing the game and i would like the variable for highscore to be "highscore", how do i code this into my game? UPDATE i have attempted to add it in but still no luck, here is the code in my init
UserHighScoreLabel = [[defaults valueForKey:#"highscore"] integerValue];
UserHighScoreLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
UserHighScoreLabel.position = ccp(65, 200);
UserHighScoreLabel.color = ccc3(255, 255, 255);
[self addChild:UserHighScoreLabel];
defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults]setInteger:Strategyscore forKey:#"highscore"];
[defaults synchronize];
You can use NSUserDefaults to save the high score on the device so it can retrieved at any time.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//saving
[defaults setValue:yourHighScore forKey:#"SavedHighScore"];
[defaults synchronize]; //you must synchronize to save
//retreiving
int highScore;
highScore = [[defaults valueForKey:#"SavedHighScore"] intValue];
//set label text to retrieved high score
UserHighScoreLabel.text = [NSString stringWithFormat:#"%d", highScore];
You could also store a JSON or XML string as the user default and parse it once you retreive it.
If you have a HighScore object or you are storing a lot of values, CoreData may be worth using, but it is not optimal if you are simply storing a few high scores.
EDIT with your code
defaults = [NSUserDefaults standardUserDefaults];
highScore = [[defaults valueForKey:#"SavedHighScore"] intValue];
//init before setting the text
UserHighScoreLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Arial" fontSize:14];
//set label text to retrieved high score
UserHighScoreLabel.text = [NSString stringWithFormat:#"%d", highScore];
UserHighScoreLabel.position = ccp(65, 200);
UserHighScoreLabel.color = ccc3(255, 255, 255);
[self addChild:UserHighScoreLabel];
Firstly fetch your last high score.
int highScore = [[defaults valueForKey:#"highscrore"] integerValue];
Now compare it with last high score if it's higher than old store then new highscore.
[NSUserDefaults standardUserDefaults]setInteger:score forKey:#"highscrore"];
[defaults synchronize];

High score and current score

I want to show on the screen the current score of the gameplay and the storical best score.
It is work, but every times i restart the game the best score change even if the current score is lower than the best score.
CCLabelTTF *punteggio;
NSString *stringa;
NSString *stringa2;
CCLabelTTF *punteggioMAX;
int score;
int scoreMAX;
There are the methods to SAVE the score, to add the score and to reset the score at the end of the game.
-(void)aum{
score++;
stringa = [NSString stringWithFormat:#"Punteggio: %d",score];
[punteggio setString:stringa];
}
-(void)res{
score=0;
stringa = [NSString stringWithFormat:#"Punteggio: %d",score];
[punteggio setString:stringa];
}
-(void)sal{
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setInteger:score forKey:#"Punteggio"];
[ud synchronize];
}
-(void)sal2{
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setInteger:scoreMAX forKey:#"Punteggio"];
[ud synchronize];
}
And in the init method:
NSString *fontName = #"score.fnt";
stringa = [NSString stringWithFormat:#"Punteggio: %d",score];
punteggio = [CCLabelBMFont labelWithString:stringa fntFile:fontName];
punteggio.scale = 0.4;
punteggio.position=ccp(40,altezzaSchermo - 15);
[self addChild:punteggio];
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
score=[ud integerForKey:#"Punteggio"];
stringa2 = [NSString stringWithFormat:#"Best Score: %d",score];
punteggioMAX = [CCLabelBMFont labelWithString:stringa2 fntFile:fontName];
punteggioMAX.scale = 0.4;
punteggioMAX.position=ccp(40,altezzaSchermo - 35);
[self addChild:punteggioMAX];
scoreMAX=[ud integerForKey:#"punteggioMAX"];
if(score>scoreMAX) scoreMAX = score;
[self res];
Thank you.
You aren't saving punteggioMAX therefore it will return 0 when you retrieve it from user defaults.
Easy to verify: set a breakpoint, check the variable.

Resources