如果我重新启动我的应用程序,则无法看到HighScore

时间:2014-04-03 22:27:56

标签: xcode

我的HighScore存在问题。当我运行应用程序时,我可以在HighScore标签中看到我的HighScore,但是当我退出我的应用程序并重新启动它时,HighScore消失了......有人可以告诉我什么是错的吗?

这是我的.h文件:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

int Y;
BOOL Start;
BOOL applications;
int RandomPosition;
int Scorenumber;
int HighScore;

@interface ViewController : UIViewController

{


SystemSoundID SoundId;


IBOutlet UILabel *Intro1;
IBOutlet UILabel *Intro2;
IBOutlet UILabel *Intro3;

IBOutlet UIImageView *Heli;

NSTimer *timer;



IBOutlet UIImageView *Obstacle;
IBOutlet UIImageView *Obstacle2;
IBOutlet UIImageView *Obstacle3;
IBOutlet UIImageView *Obstacle4;
IBOutlet UIImageView *Obstacle5;
IBOutlet UIImageView *Obstacle6;
IBOutlet UIImageView *Obstacle7;
IBOutlet UIImageView *Obstacle8;
IBOutlet UIImageView *Obstacle9;
IBOutlet UIImageView *Bottom1;
IBOutlet UIImageView *Bottom2;
IBOutlet UIImageView *Top2;
IBOutlet UIImageView *Top1;
IBOutlet UIImageView *corona;

IBOutlet UILabel *Score;

NSTimer *Scorer;



}






@end

这是我的.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(void)Collision{


if (CGRectIntersectsRect(Heli.frame, Obstacle.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Obstacle2.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Bottom1.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Top1.frame)) {
    [self EndGame];
}





}


-(void)EndGame{

if (Scorenumber > HighScore) {
    HighScore = Scorenumber;
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber  
numberWithInt:HighScore] forKey:@"HighScoreSaved"];
}


Heli.hidden = YES;
[timer invalidate];
[Scorer invalidate];


[self performSelector:@selector(NewGame) withObject: nil afterDelay:2];

}


-(void)NewGame{

Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;

Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;

Heli.hidden = NO;
Heli.center = CGPointMake(88, 286);
Heli.image = [UIImage imageNamed:@"buss til app opp.png"];

Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:@"Score: 0"];
Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore];


}







-(void)HeliMove{

[self Collision];

Heli.center = CGPointMake(Heli.center.x, Heli.center.y + Y);

Obstacle.center = CGPointMake(Obstacle.center.x -5, Obstacle.center.y);
Obstacle2.center = CGPointMake(Obstacle2.center.x -5, Obstacle2.center.y);
Bottom1.center = CGPointMake(Bottom1.center.x -5, Bottom1.center.y -0);
Top1.center = CGPointMake(Top1.center.x -5, Top1.center.y -0);
corona.center = CGPointMake(corona.center.x -5, corona.center.y );


if (Obstacle.center.x < -70) {
    RandomPosition = arc4random() %185;
    RandomPosition = RandomPosition + 170;
    Obstacle.center = CGPointMake(300, RandomPosition);
}


if (Obstacle2.center.x < 0) {
    RandomPosition = arc4random() %165;
    RandomPosition = RandomPosition + 600;
    Obstacle.center = CGPointMake(300, RandomPosition);
}


if (Top1.center.x <-100) {
    RandomPosition = arc4random() %80;
    Top1.center = CGPointMake(400, 17);
    RandomPosition = RandomPosition + 495;
    Bottom1.center = CGPointMake(400, 540);
}



if (corona.center.x < -3000) {
    RandomPosition = arc4random() %165;
    RandomPosition = RandomPosition + 110;
    corona.center = CGPointMake(6000, 162);
}




}



-(void)Scoring{
Scorenumber = Scorenumber + 1;
Score.text = [NSString stringWithFormat:@"Score: %i", Scorenumber];

if (CGRectIntersectsRect(Heli.frame, corona.frame)) {
    Scorenumber = Scorenumber + 20;
    corona.hidden = YES;

}




}





-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{



if (Start == YES) {

    AudioServicesPlaySystemSound(SoundId);

    Intro1.hidden = YES;
    Intro2.hidden = YES;
    Intro3.hidden = YES;

    timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self 
selector:@selector(HeliMove) userInfo:nil repeats:YES];

    Scorer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self     
 selector:@selector(Scoring) userInfo:nil repeats:YES];


    Start = NO;

    Bottom1.hidden = NO;
    Top1.hidden = NO;
    Obstacle.hidden = NO;
    Obstacle2.hidden = NO;
    Obstacle3.hidden = NO;
    Obstacle4.hidden = NO;
    Obstacle5.hidden = NO;
    Obstacle6.hidden = NO;
    Obstacle7.hidden = NO;
    Obstacle8.hidden = NO;
    Obstacle9.hidden = NO;
    corona.hidden = NO;


    RandomPosition = arc4random() %150;
    RandomPosition = RandomPosition + 397;
    Obstacle.center = CGPointMake(570, RandomPosition);

    RandomPosition = arc4random() %75;
    RandomPosition = RandomPosition + 259;
    Obstacle2.center = CGPointMake(855, RandomPosition);


    RandomPosition = arc4random() %55;
    corona.center = CGPointMake(1040, 162);



}

Y = -7;
Heli.image = [UIImage imageNamed:@"buss til app opp.png"];

} 


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

Y = 7;
Heli.image = [UIImage imageNamed:@"buss til app ned.png"];


}


- (void)viewDidLoad
{

    // Get your highscore from the prefs.
    HighScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"]       
intValue ];

    Start = YES;
    Bottom1.hidden = YES;
    Top1.hidden = YES;
    Obstacle.hidden = YES;
    Obstacle2.hidden = YES;
    Obstacle3.hidden = YES;
    Obstacle4.hidden = YES;
    Obstacle5.hidden = YES;
    Obstacle6.hidden = YES;
    Obstacle7.hidden = YES;
    Obstacle8.hidden = YES;
    Obstacle9.hidden = YES;
    corona.hidden = YES;



[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

如果有人可以回答这个问题真有帮助:) 我和它坐在一起后,我有点厌倦了!

1 个答案:

答案 0 :(得分:1)

使用密钥将您的高分放入NSUserDefaults:

forKey:@"HighScoreSaved"];

但是你试图用以下方法解决这个问题:

objectForKey:@"HighScore"] 

您的密钥在两个地方都必须相同。或者换句话说,您必须使用相同的密钥写入和读取NSUserDefaults。