我有物品。一切都是开放的,除了10.开放级别10应该在任何5个完成?这是我的截图http://pixs.ru/showimage/Snimokekra_9304644_10796687.png。
如何使用NSUserDefaults standardUserDefaults保存已完成的级别? 我只是初学者,我无法理解一些事情。如果你能帮助我,我将非常感激,我会尽力详细解释。
谢谢
CCMenu *menuLevel_1 = [CCMenu menuWithItems:menuLevel_1_Item, nil];
menuLevel_1.position = ccp(size.width/9,size.height/1.4);
[self addChild:menuLevel_1 z:3];
CCLabelTTF *label_1 = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"1"] fontName:@"Avenir" fontSize:35];
label_1.position = ccp(menuLevel_1_Item.contentSize.width/2, menuLevel_1_Item.contentSize.height/2);
[menuLevel_1_Item addChild:label_1];
CCMenuItemSprite *menuLevel_2_Item = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"button_level"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"button_level_clicked"] target:self selector:@selector(Level_2_Scene:)];
CCMenu *menuLevel_2 = [CCMenu menuWithItems:menuLevel_2_Item, nil];
menuLevel_2.position = ccp(size.width/4,size.height/1.4);
[self addChild:menuLevel_2 z:3];
CCLabelTTF *label_2 = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"2"] fontName:@"Avenir" fontSize:35];
label_2.position = ccp(menuLevel_2_Item.contentSize.width/2, menuLevel_2_Item.contentSize.height/2);
[menuLevel_2_Item addChild:label_2];
.........
CCMenuItemSprite *menuLevel_10_Item = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"button_level_lock"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"button_level_clicked"] target:self selector:@selector(Level_10_Scene:)];
CCMenu *menuLevel_10 = [CCMenu menuWithItems:menuLevel_10_Item, nil];
menuLevel_10.position = ccp(size.width/1.5,size.height/1.8);
[self addChild:menuLevel_10 z:3];
答案 0 :(得分:0)
你不应该使用NSUserDefaults,创建一个自定义的GameState类,如果内容不是太大,使用NSCoding应该做的工作。
这是一个GameState类的例子。
首先,你需要一个Class来处理存储
数据库类标题
#import <Foundation/Foundation.h>
id loadData(NSString * filename);
void saveData(id theData, NSString *filename);
数据库类实施
#import "GCDatabase.h"
NSString * pathForFile(NSString *filename) {
// 1
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
// 2
NSString *documentsDirectory = [paths objectAtIndex:0];
// 3
return [documentsDirectory
stringByAppendingPathComponent:filename];
}
id loadData(NSString * filename) {
// 4
NSString *filePath = pathForFile(filename);
// 5
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// 6
NSData *data = [[[NSData alloc]
initWithContentsOfFile:filePath] autorelease];
// 7
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc]
initForReadingWithData:data] autorelease];
// 8
id retval = [unarchiver decodeObjectForKey:@"Data"];
[unarchiver finishDecoding];
return retval;
}
return nil; }
void saveData(id theData, NSString *filename) {
// 9
NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
// 10
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc]
initForWritingWithMutableData:data] autorelease];
// 11
[archiver encodeObject:theData forKey:@"Data"];
[archiver finishEncoding];
// 12
[data writeToFile:pathForFile(filename) atomically:YES];
游戏状态标题
#import <Foundation/Foundation.h>
@interface GameState : NSObject <NSCoding> {
BOOL completedLevel1;
BOOL completedLevel2;
BOOL completedLevel3;
BOOL completedLevel4;
BOOL completedLevel5;
int timesFell;
}
+ (GameState *) sharedInstance;
- (void)save;
@property (assign) BOOL completedLevel1;
@property (assign) BOOL completedLevel2;
@property (assign) BOOL completedLevel3;
@property (assign) BOOL completedLevel4;
@property (assign) BOOL completedLevel5;
@property (assign) int timesFell;
@end
游戏状态实施
#import "GameState.h"
#import "GCDatabase.h"
@implementation GameState
@synthesize completedLevel1;
@synthesize completedLevel2;
@synthesize completedLevel3;
@synthesize completedLevel4;
@synthesize completedLevel5;
@synthesize timesFell;
static GameState *sharedInstance = nil;
+(GameState*)sharedInstance {
@synchronized([GameState class])
{
if(!sharedInstance) {
sharedInstance = [loadData(@"GameState") retain];
if (!sharedInstance) {
[[self alloc] init];
}
}
return sharedInstance;
}
return nil; }
+(id)alloc {
@synchronized ([GameState class])
{
NSAssert(sharedInstance == nil, @"Attempted to allocate a \
second instance of the GameState singleton");
sharedInstance = [super alloc];
return sharedInstance;
}
return nil; }
- (void)save {
saveData(self, @"GameState");
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeBool:completedLevel1 forKey:@"CompletedLevel1"];
[encoder encodeBool:completedLevel2 forKey:@"CompletedLevel2"];
[encoder encodeBool:completedLevel3 forKey:@"CompletedLevel3"];
[encoder encodeBool:completedLevel4 forKey:@"CompletedLevel4"];
[encoder encodeBool:completedLevel5 forKey:@"CompletedLevel5"];
[encoder encodeInt:timesFell forKey:@"TimesFell"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if ((self = [super init])) {
completedLevel1 = [decoder
decodeBoolForKey:@"CompletedLevel1"];
completedLevel2 = [decoder
decodeBoolForKey:@"CompletedLevel2"];
completedLevel3 = [decoder
decodeBoolForKey:@"CompletedLevel3"];
completedLevel4 = [decoder
decodeBoolForKey:@"CompletedLevel4"];
completedLevel5 = [decoder
decodeBoolForKey:@"CompletedLevel5"];
timesFell = [decoder decodeIntForKey:@"TimesFell"];
}
return self;
}