iPhone游戏该如何保存
2010-05-11 16:31:00 来源:WEB开发网// code the length of the event list (progress through the level)
NSNumber *listCount = [NSNumber numberWithInt: [eventList count]];
[coder encodeObject: listCount forKey:@"eventList.count" ];
// code the list of players
[coder encodeObject: charList forKey:@"charList" ];
}
Notice I didn’t save anything about the structure of the map or the what it looks like ? that’s all static data I can get elsewhere. When I load this object I’ll know the level name, and I can get all of the static data from there.
What about the positions of all of the players? Well, you can see that I’m encoding “charList” above, which is an NSArray of the players. That means encodeWithCoder will be called for each item in that array, so I leave it up to the player class to save the important data for each player. Here’s the code for saving a player:
//Player.m
//encode a player or monster character
- (void) encodeWithCoder: (NSCoder *)coder
{
//save type number
[coder encodeObject: [NSNumber numberWithInt:type] forKey:@”type” ];
//save x and y position
[coder encodeObject:
[NSNumber numberWithFloat:position.x] forKey:@”position.x” ];
[coder encodeObject:
[NSNumber numberWithFloat:position.y] forKey:@”position.y” ];
//save health
[coder encodeObject: [NSNumber numberWithInt: health] forKey:@”health” ];
}
You can see that there are many things I save, but there’s more data that I don’t save. I don’t save the maximum health of a character, or weapon range, or firing rate, or information about how he is drawn ? that info is already in the default constructor, and there is no need to save it redundantly. Avoiding redundancy helps speed up the save process, and it should reduce bugs ? a player
更多精彩
赞助商链接