Reputation: 79
I want that my SKScene
(MedalScene
) recognizes the high score of the GameScene
because if the high score is for example "20" then it will appear a medal. How I can do that?
My code in the GameScene for the high score:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:[NSString stringWithFormat:@"%d",_score] forKey:SCORE_OVER];
int scoreString=[[ud objectForKey:BEST_SCORE] intValue];
if(scoreString < _score){
[ud setObject:[NSString stringWithFormat:@"%d",_score] forKey:BEST_SCORE];
scoreString = _score;
And this is the currently code of my MedalScene
for the medals:
if(scoreString >= 20){
SKSpriteNode *medal1 = [SKSpriteNode spriteNodeWithImageNamed:@"medal_1"];
medal1.position = CGPointMake(CGRectGetMaxX( self.frame )*1/4 + 8, CGRectGetMidY(self.frame) + 21);
[self addChild:medal1];
}
My GameScene shows the GameOver with the score as well as the high score. And this are the codes:
-(void)showGameOver{
NSUserDefaults *hud = [NSUserDefaults standardUserDefaults];
[hud setObject:[NSString stringWithFormat:@"%d",_score] forKey:SCORE_OVER];
int scoreString = [[hud objectForKey:BEST_SCORE] intValue];
if(scoreString < _score){
[hud setObject:[NSString stringWithFormat:@"%d",_score] forKey:BEST_SCORE];
scoreString = _score;
SKLabelNode *lbOver = [SKLabelNode labelNodeWithFontNamed:@"Walibi0615"];
lbOver.position = CGPointMake(CGRectGetMaxX(self.frame)*3/4, CGRectGetMidY(self.frame)+33);
lbOver.zPosition = 1;
lbOver.fontSize = 20;
lbOver.fontColor = [UIColor blackColor];
lbOver.text = [NSString stringWithFormat:@"%d", _score];
[_gameOver addChild:lbOver];
SKLabelNode *lbMaxOver = [SKLabelNode labelNodeWithFontNamed:@"Walibi0615"];
lbMaxOver.position = CGPointMake(CGRectGetMaxX(self.frame)*3/4, CGRectGetMidY(self.frame)-14);
lbMaxOver.zPosition = 1;
lbMaxOver.fontSize = 20;
lbMaxOver.fontColor = [UIColor blackColor];
lbMaxOver.text = [NSString stringWithFormat:@"%d", scoreString];
[_gameOver addChild:lbMaxOver]; }
I have created a second scene (MedalScene) and this scene should recognize the scoreString of the GameScene because if for example, the high score is equal to 20 then it will appear a medal in this scene.
And this is the currently code for the medal (MedalScene):
if(scoreString >= 20){
SKSpriteNode *medal1 = [SKSpriteNode spriteNodeWithImageNamed:@"medal_1"];
medal1.position = CGPointMake(CGRectGetMaxX( self.frame )*1/4 + 8, CGRectGetMidY(self.frame) + 21);
[self addChild:medal1]; }
I hope now it is clear what I mean. Thank you in advance for your help! :)
Upvotes: 1
Views: 71
Reputation: 17725
You should synchronize your userdefaults after you set them.
[ud synchronize];
I'd use real integers inside your userdefaults too though (i.s.o. hand-converting to strings), so something like
NSInteger score = [ud integerForKey:BEST_SCORE];
if (_score > score) {
[ud setInteger:_score forKey:BEST_SCORE];
[ud synchronize];
}
Upvotes: 1