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

iPhone游戏该如何保存

 2010-05-11 16:31:00 来源:WEB开发网   
核心提示:对于任何做iPhone游戏的开发者,如何保存游戏是一个必需解决的问题,iPhone游戏该如何保存,这里转一篇非常赞的文章,里面简单明了地介绍了解决办法,我懒得翻译了,如果你更懒以至于不愿意读英文说明的话

对于任何做iPhone游戏的开发者,如何保存游戏是一个必需解决的问题。这里转一篇非常赞的文章,里面简单明了地介绍了解决办法。我懒得翻译了,如果你更懒以至于不愿意读英文说明的话,可以直接看代码,挺清晰的。

原文链接:http://deadpanic.com/howtosave

How to save your game (or any object) on the iPhone

Warning to some, bonus to others: this post is less markety and more code-heavy. It also reveals some info about the architecture of “Dead Panic.”

Newcomers to iPhoneDevSDK often ask how to convert a string to bytes for writing to a file and other questions about data persistence that belie their real question: how do I save an object and restore it at a later time? NSUserDefaults is fine for storing some strings or bools, but what about an entire custom object? This is called “archiving” and the Apple docs are here.

In short, as long as an object conforms to the NSCoding protocol you can save it to a file just by calling archiveRootObject:toFile:. Most built-in objects already conform to the protocol; you can save an entire NSArray of NSStrings, NSNumbers, and NSDictiories with that one line of code. If you want to save your own custom objects, though, you must make them conform to the NSCoder protocol by adding two methods ? encodeWithCoder and initWithCoder.

How to save an object

In “Dead Panic” I instantly save the game whenever they press the home button, and restore their saved game whenever the game is launched again. To do that I need to save some information about which map they’re on, the player’s progress, and the player and enemy positions and health.

Here is my code for saving the map to a coder:

//Map.m

//encode the map data

- (void) encodeWithCoder: (NSCoder *)coder

{

//code the level name

[coder encodeObject: currentLevelName forKey:@"currentLevelName" ];

1 2 3 4 5  下一页

Tags:iPhone 游戏 如何

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读