High score and current score - ios

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.

Related

NSUserDefaults standardUserDefaults synchronize gives wrong result objective c

I want to save high score using
synchronize
in my app,but i get gibberish number. Here what i got :
GameScene.m
if (highScore <score) {
highScore = score;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:highScore forKey:#"highScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
GameOver.m
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
NSString *rezult =[prefs stringForKey:#"highScore"];
SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:#"Menlo-Bold"];
highscorelabel.text =[NSString stringWithFormat:#"%ld",(long)rezult];
highscorelabel.position = CGPointMake(CGRectGetMidX(self.frame), 280);
highscorelabel.fontSize = 45;
highscorelabel.zPosition = 5;
[self addChild:highscorelabel];
Why I'm getting wrong value and how to fix this problem?
You're getting a wrong value because rezult is a NSString, you should change this:
NSString *rezult =[prefs stringForKey:#"highScore"];
to this:
NSInteger rezult = [prefs integerForKey:#"highScore"];
try to don't init another nsuserdefaults every time just use
[[NSUserDefaults standardUserDefaults] setValue:surname.text forKey:#"Nume"]; to write
[[NSUserDefaults standardUserDefaults] valueForKey:#"Name"] to read
AND MAKE SURE YOU USE THE SAME OBJECT TYPE!

How to make high score save in game? xcode

I am making a small game for iOS in Xcode. In my game, tapping on a dot will give you one point. I am trying to make it so that if your score is higher than the first high score(0), it should save that and display the new high score. And the next score can beat that, and so on. Please give a thorough explanation because I am new to xCode and I am a 12 year old app developer.
If your do not find your score data important or sensitive use NSUserDefaults
First scene: (Setting the integer)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:yourHighScore forKey:#"HighScore"];
Other scenes: (Getting the integer)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
yourHighScore = [defaults integerForKey:#"HighScore"]
Or same scene setting the yourHighScore value: (Getting the integer)
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
yourHighScore = [defaults integerForKey:#"HighScore"]
}
NSInteger HighScore;
int Score;
IBOutlet UILabel *Show;
-(void)ViewDidLoad
{
[super viewDidLoad];
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScoreSaved"];
}
-(void)ShowHighScore
{
if (Score > HighScore) {
HighScore = Score;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:#"HighScoreSaved"];
}
Show.text = [NSString stringWithFormat:#"%li", HighScore];
}

Always getting 0 for integer retrieved from NSUserDefaults

I have an integer stored in user defaults for my game's high score. Every time I start the game I want to check if the score is bigger than the previous high score, so I do this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
if (highScoreNum <= score) {
highScoreNum = score;
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
else if (highScoreNum > score) {
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
and at the place where I declare the variable I do this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScore forKey:#"highScoreNumber"];
highLabel.text = [NSString stringWithFormat:highScore];
but I always get 0 in highScoreNum.
Every time you do this...
[defaults setInteger:highScore forKey:#"highScoreNumber"];
...you send the highScore variable to the defaults. If you declare highScore, but then don't fetch a previously saved default (or don't otherwise initialize highScore), then it makes sense why you keep seeing zero here. You probably keep sending nil to the defaults, which go in (and come back out) as an integer value of zero.
Immediately after you declare *defaults, instead of calling setInteger:forKey, you probably want to have highScore store the appropriate value from the defaults, which looks like this:
highScore = [defaults integerFromKey:#"highScoreNumber"];
Also, don't forget to [defaults synchronize] when appropriate.
setInteger:forKey::
Sets the value of the specified default key to the specified integer
value.
So your code stores a value to the NSUserDefaults, in several places. It never reads a value back.
(and you should look up -synchronize)
Try this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
[defaults synchronize]; // force the upgrade NSUserDefaults
if (highScoreNum <= score) {
highScoreNum = score;
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
else if (highScoreNum > score) {
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
[defaults synchronize]; // force the upgrade NSUserDefaults
}

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];

Resources