Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode Can';如果我重新启动我的应用程序,我看不到高分_Xcode - Fatal编程技术网

Xcode Can';如果我重新启动我的应用程序,我看不到高分

Xcode Can';如果我重新启动我的应用程序,我看不到高分,xcode,Xcode,我的高分有问题。当我运行完应用程序后,我可以在HighScore标签中看到我的HighScore,但是当我退出应用程序并重新启动它时,HighScore消失了。。。谁能告诉我怎么了 这是我的.h文件: #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> int Y; BOOL Start; BOOL applications; int RandomPosition; int Scorenumber;

我的高分有问题。当我运行完应用程序后,我可以在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
#导入
#进口
int-Y;
BOOL启动;
布尔应用程序;
int随机位置;
整数;
int高分;
@界面ViewController:UIViewController
{
系统声音ID;
IBUILabel*简介1;
IBUILabel*简介2;
IBUILabel*简介3;
IBUIImageView*直升机;
n定时器*定时器;
IBUIImageView*障碍物;
IBUIImageView*Obstacle2;
IBUIImageView*Obstacle3;
IBUIImageView*Obstacle4;
IBUIImageView*障碍物5;
IBUIImageView*障碍6;
IBUIImageView*障碍7;
IBUIImageView*障碍8;
IBUIImageView*Obstacle9;
IBUIImageView*Bottom1;
IBUIImageView*Bottom2;
IBUIImageView*Top2;
IBUIImageView*Top1;
IBUIImageView*电晕;
IBUILabel*分数;
NSTimer*记分员;
}
@结束
这是我的.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
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)碰撞{
if(CGRectIntersectsRect(直升机框架、障碍物框架)){
[自我终结];
}
if(CGRectIntersectsRect(Heli.frame,Obstacle2.frame)){
[自我终结];
}
if(CGRectIntersectsRect(直升机机架,底部1机架)){
[自我终结];
}
if(CGRectIntersectsRect(Heli.frame,Top1.frame)){
[自我终结];
}
}
-(无效)终局{
如果(分数编号>高分){
高分=分数编号;
[[NSUserDefaults standardUserDefaults]设置对象:[NSNumber]
numberWithInt:HighScore]forKey:@“HighScore已保存”];
}
Heli.hidden=是;
[计时器失效];
[记分员无效];
[自执行选择器:@selector(NewGame)with object:nil afterDelay:2];
}
-(无效)新游戏{
Bottom1.hidden=是;
Top1.hidden=是;
障碍物。隐藏=是;
Obstacle2.hidden=是;
corona.hidden=是;
Intro1.hidden=NO;
Intro2.hidden=NO;
Intro3.hidden=NO;
Heli.hidden=否;
Heli.center=CGPointMake(88286);
Heli.image=[UIImage ImageName:@“buss til app opp.png”];
开始=是;
分数=0;
Score.text=[NSString stringWithFormat:@“Score:0”];
Intro3.text=[NSString stringWithFormat:@“HighScore:%i”,HighScore];
}
-(无效)直升机移动{
[自碰撞];
Heli.center=CGPointMake(Heli.center.x,Heli.center.y+y);
障碍物.center=CGPointMake(障碍物.center.x-5,障碍物.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);
如果(障碍物中心x<-70){
RandomPosition=arc4random()%185;
随机位置=随机位置+170;
障碍物中心=CGPointMake(300,随机位置);
}
如果(障碍物2.center.x<0){
RandomPosition=arc4random()%165;
随机位置=随机位置+600;
障碍物中心=CGPointMake(300,随机位置);
}

如果(Top1.center.x您将高分输入NSUserDefaults,则按键:

forKey:@"HighScoreSaved"];
但你正试图用以下方法来解决问题:

objectForKey:@"HighScore"] 
您的密钥需要在两个位置都相同。或者换句话说,您必须使用相同的密钥向NSUserDefaults进行写入和读取