iPhone游戏该如何保存
2010-05-11 16:31:00 来源:WEB开发网How to load an object
Of course we need to load the objects too; here is the code for that
//Map.m
//init a map from a coder
- (id) initWithCoder: (NSCoder *) coder
{
[self init];
// load the level name
self.currentLevelName = [coder decodeObjectForKey:@"currentLevelName"];
// load the current event number
int eventsRemaining = [[coder decodeObjectForKey:@"eventList.count"] intValue];
// load level based on level name
[self loadLevel: currentLevelName];
//skip events until we get to the current event number
int length = [eventList count];
NSRange deletionRange = NSMakeRange(0, length-eventsRemaining);
[eventList removeObjectsInRange:deletionRange];
//get the list of characters
NSArray *tempCharList = [coder decodeObjectForKey:@"charList"];
//add chars to map, set team counts
for (id newChar in tempCharList)
[self addChar: newChar];
return self;
}
There are some tricks to look at there. I call the default init for the class, then I load a level based on the current level name. This is the same loadLevel method that I use when starting a normal game ? I try to keep the custom code for loaded games to a minimum. After I load the level I remove items from the event stack until it matches the length of the saved stack. I could have saved the event stack instead, but again, I’m trying not to duplicate any data.
I also restore the list of characters, but then I add them one at a time using another method of this object ? that’s because I have some custom code that must be run for every object on the map. Rather than duplicate that code, I call the method the same way it would be called in regular gameplay.
更多精彩
赞助商链接