How to fix the maximum score in the game and memorize it? sprite kit, obj-c

  • Read to start with how to ask questions , your question is completely incomprehensible. - Yuriy Orlov

2 answers 2

Obviously, at a certain time, you need to take the available maximum result and write it into long-term memory. Read if necessary. For example, to create a file, when starting the program and having a file read data from it, write out the necessary information in the future when exiting the program.

A sprite kit and other details of the implementation of the game do not matter here.

UPD. Example. Preservation:

NSMutableDictionary *dict = [NSMutableDictionary new]; [dict setValue:[NSNumber numberWithInt:10] forKey:@"MaxScores"]; [dict setValue:[NSNumber numberWithDouble:1.0] forKey:@"SoundVolume"]; [dict setValue:@"Чувак" forKey:@"PlayerName"]; NSData *dataRep = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:NULL]; [dataRep writeToFile:filename atomically:YES]; 

Loading:

 NSData *dataRep = [NSData dataWithContentsOfFile:filename]; NSDictionary *dict = [NSPropertyListSerialization propertyListWithData:dataRep options:NSPropertyListImmutable format:NULL error:NULL]; 
  • Can you learn more about creating a file? .plist or something like that? Thanks - pbogdanv
  • I would use NSData. You can stuff it into what you want - it is convenient to store the program settings. For example, a dictionary. Record: NSDictionary * propertyList = <create and populate> NSData * dataRep = [NSPropertyListSerialization dataWithPropertyList: propertyList format: NSPropertyListXMLFormat_v1_0 options: NSPropertyListImmutable error: NULL]; [dataRep writeToFile: filename atomically: YES]; Load: NSData * dataRep = [NSData dataWithContentsOfFile: filename]; NSDictionary * propertyList = [NSPropertyListSerialization propertyListWithData: dataRep options: NSPropertyListImmutable format: NULL error: NULL]; - AivanF.
  • thank! I try - pbogdanv
  • @pbogdanv here do not say thanks, but put a tick on the answer;) - AivanF.

NSUserDefaults is your choice.

Save:

 [[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"highScore"]; 

Download:

 score = [[NSUserDefaults standardUserDefaults] integerForKey:@"highScore"];