[英]Saving High Scores with SpriteKit and iOS 7
我正在創建一個應用,該應用需要存儲多個不同級別的高分。 我在http://www.raywenderlich.com/63235/how-to-save-your-game-data-tutorial-part-1-of-2中使用了出色的教程,以此作為我所依據的基礎試圖做。 根據他們的建議,我使用了https://gist.github.com/dhoerl/1170641的keychainwrapper
在我的Xcode之外的iOS sim卡中,保存游戲狀態的工作非常正常,我可以關閉Xcode,甚至重新啟動Mac,高分會像應該的那樣顯示。 但是,當我使用iPhone 5作為目的地時,它根本無法工作。 如果我殺死了該應用程序,那么高分就消失了。 不知道這筆交易我缺少什么。
這是我的gameState.h:
#import <Foundation/Foundation.h>
@interface GameState : NSObject <NSCoding>
@property (assign, nonatomic) long score;
@property (assign, nonatomic) long highScoreL1;
@property (assign, nonatomic) long highScoreL2;
@property (nonatomic) long levelIndex;
@property (nonatomic) long lvlIndexMax;
+(instancetype)sharedGameData;
-(void)reset;
-(void)resetAll;
-(void)save;
@end
這是我的gameState.m
#import "GameState.h"
#import "KeychainWrapper.h"
@implementation GameState
static NSString* const SSGameDataChecksumKey = @"SSGameDataChecksumKey";
static NSString* const SSGameDataHighScoreL1Key = @"highScoreL1";
static NSString* const SSGameDataHighScoreL2Key = @"highScoreL2";
-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeDouble:self.highScoreL1 forKey:SSGameDataHighScoreL1Key];
[encoder encodeDouble:self.highScoreL2 forKey:SSGameDataHighScoreL2Key];
}
-(instancetype)initWithCoder:(NSCoder *)decoder {
self = [self init];
if (self) {
_highScoreL1 = [decoder decodeDoubleForKey:SSGameDataHighScoreL1Key];
_highScoreL2 = [decoder decodeDoubleForKey:SSGameDataHighScoreL2Key];
}
return self;
}
+(NSString*)filePath {
static NSString* filePath = nil;
if (!filePath) {
filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingString:@"gamedata"];
}
return filePath;
}
+(instancetype)loadInstance {
NSData* decodedData = [NSData dataWithContentsOfFile: [GameState filePath]];
if (decodedData) {
NSString* checksumOfSavedFile = [KeychainWrapper computeSHA256DigestForData:decodedData];
NSString* checksumInKeychain = [KeychainWrapper keychainStringFromMatchingIdentifier:SSGameDataChecksumKey];
if ([checksumOfSavedFile isEqualToString: checksumInKeychain]){
GameState* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
return gameData;
}
}
return [[GameState alloc]init];
}
-(void)save {
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:[GameState filePath] atomically:YES];
NSString* checksum = [KeychainWrapper computeSHA256DigestForData: encodedData];
if ([KeychainWrapper keychainStringFromMatchingIdentifier:SSGameDataChecksumKey]) {
[KeychainWrapper updateKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
} else {
[KeychainWrapper createKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
}
}
+(instancetype)sharedGameData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
-(void)reset {
self.score = 0;
}
-(void)resetAll {
self.score = 0;
//self.highScoreL1 = 0;
//self.highScoreL2 = 0;
}
@end
在每個級別中,我都會跟蹤分數,然后當用戶輸掉游戲時,在進入“游戲結束”場景之前,我使用以下行來更新高分:
[GameState sharedGameData].highScoreL1 = MAX([GameState sharedGameData].score, [GameState sharedGameData].highScoreL1);
最后,在Game Over內,我使用
[[GameState sharedGameData] save];
保存高分。
在此先感謝您的幫助,任何方向都將是美好的。 讓我知道您是否需要更多信息!
在不同場景之間保存本地數據的最安全,最快的方法是使用nsuserdefaults例如
//靜態變量
static NSString * topScore= @"topScore";
-(void)setTopscore:(long long)newScore
{
NSString *newhighScore = [NSString stringWithFormat:@"%lld", (long long)newScore];
[_userlocalData setObject:newhighScore forKey:topScore];
[_userlocalData synchronize];
}
//get top score from local database storage====================================================================//
-(NSString*)getTopscore
{
NSString *newhighScore = [_userlocalData stringForKey:topScore];
return newhighScore;
}
我嘗試了它,並且在所有iOS設備和Mac本身上都沒有任何問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.