Ios 在tap me游戏中记录游戏分数
IOS游戏 我正在创建一个游戏,如果有一个按钮,游戏的目的是看看在时间用完之前你能得到多少点击 但我有一个问题,我需要重新记录的高分,我怎么能做到这一点 .h代码Ios 在tap me游戏中记录游戏分数,ios,Ios,IOS游戏 我正在创建一个游戏,如果有一个按钮,游戏的目的是看看在时间用完之前你能得到多少点击 但我有一个问题,我需要重新记录的高分,我怎么能做到这一点 .h代码 #import <UIKit/UIKit.h> @interface errrViewController : UIViewController{ IBOutlet UILabel *label; IBOutlet UILabel *timerLabel; NSInteger count; NSInteger sec
#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
请分步骤说明如何执行此操作
如果可能,请提供任何可以帮助我的链接。您可以通过以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;
}
您还可以使用load highscore功能,如:
-(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
函数中
希望这能有所帮助。您需要更好地描述您尝试过的内容以及问题所在。