WEB开发网
开发学院手机开发iPhone 开发 iPhone游戏该如何保存 阅读

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: listCoun

// 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

上一页  1 2 3 4 5  下一页

Tags:iPhone 游戏 如何

编辑录入:coldstar [复制链接] [打 印]
赞助商链接