IOS游戏 我正在创建一个游戏,如果有一个按钮,游戏的目标是在时间用完之前看到你可以获得多少水龙头
但我有一个问题,我需要记录高分,我怎么能这样做。
.h代码
#import <UIKit/UIKit.h>
@interface errrViewController : UIViewController{
IBOutlet UILabel *label;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer; //ADD THIS!!
}
- (IBAction) buttonPressed;
- (void)setupGame;
- (void)subtractTime;
@end
米。代码
#import "errrViewController.h"
@interface errrViewController ()
@end
@implementation errrViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Implementing our method
- (IBAction)buttonPressed{
count++;
label.text = [NSString stringWithFormat:@"Score\n%i",count];
}
- (void)setupGame{
seconds = 30;
count = 0;
timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
label.text = [NSString stringWithFormat:@"Score\n%i",count];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(subtractTime) userInfo:nil repeats:YES];
}
- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
if(seconds == 0){
[timer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!" message:[NSString stringWithFormat:@"You scored %i points",count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self setupGame];
}
@end
#import "errrViewController.h"
@interface errrViewController ()
@end
@implementation errrViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Implementing our method
- (IBAction)buttonPressed{
count++;
label.text = [NSString stringWithFormat:@"Score\n%i",count];
}
- (void)setupGame{
seconds = 30;
count = 0;
timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
label.text = [NSString stringWithFormat:@"Score\n%i",count];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(subtractTime) userInfo:nil repeats:YES];
}
- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
if(seconds == 0){
[timer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!" message:[NSString stringWithFormat:@"You scored %i points",count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self setupGame];
}
@end
请按步骤说明如何执行此操作
如果可能,请提供任何可以帮助我的链接
答案 0 :(得分:0)
您可以通过以NSDictionary的形式将高分保存到plist文件来完成此操作。例如:
int highscore; //your highscore variable, which I believe you can calculate yourself
然后你可以有一个保存功能,如:
-(void) saveHighscore{
NSDictionary* highscoreDict = [[NSDictionary alloc] initWithObjectsAndKeys: [NSString stringWithFormat:@"%d", highscore], @"Highscore",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//get the path for the documents directory
NSString* filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"Highscore.plist"];
//Check if file exists
if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
//delete it if it does, because we're going to replace it anyway
[[NSFileManager defaultManager]removeItemAtPath:filePath error:nil];
}
[highscoreDict writeToFile:filePath atomically:YES]; // write to file
}
你可以拥有一个负载高分函数,如:
-(int) loadHighscore{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"Highscore.plist"];
//Check if file exists
if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
NSDictionary* highscoreDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
//Retrieve the value of highscore from your file
return [[highscoreDict valueForKey: @"Highscore"]intValue];
}
//Otherwise return 0 as the highscore
return 0;
}
在[self loadHighscore];
函数中添加viewDidLoad
。
希望这有帮助。