Reading and storing data in a game (iPhone)
I'm currently trying to learn game programming, so I'm starting with a simple application for the iPhone so that I can sort of learn the flow and organization of game programming. Right now, I have a class for the player's ship called PlayerShip and the PlayerShip can come in different levels (i.e. when the player upgrades their ship). So for each level of ship, the class uses a different image. For each level of ship there are constant values that I have such as the number of turrets on the ship, the location of those turrets, and the number of thrusters and location of those thrusters on the ships (e.g. ship level 1 has two turrets开发者_如何学编程 and one thruster). My question is, what would be the best way to store and read in this data for the ships? I don't want to have to define them each time I initiate a ship because that seems unorganized, and I also would not like to hard code it in case of changes. I was thinking of using a PLIST file to store all of the values, but I also know that when reading a PLIST it's all or nothing meaning you have to read the entire file into memory and I don't want that to hit the game's performance. So what would the best route for storing this static information and having it be retrievable in the code be without having to risk performance?
If you don't hard code you values into the class, then you will have to read them from some external source (which can affect performance - depending on how and when you do it). In your case, it sounds like your best bet is to load the information "upfront" (such as when the app starts up). Then it will remain in memory until the program is terminated.
As far as how to store the data, you may consider looking into Core Data:
http://developer.apple.com/library/ios/ipad/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html
Of particular interest to you will be how to ship you app with pre loaded data:
http://www.raywenderlich.com/980/core-data-tutorial-how-to-preloadimport-existing-data
Firstly you have to consider "...hard code it in case of changes..." The problem you have with changes are that with every change in your app binary you need to upload a new version to the app store (this includes changes to plist etc in the app bundle).
With that being said you could choose to download a plist (or other file i.e. xml etc) with these values from a web server when your app starts. In your case you would be better off to load these values when you initialize your game and keep them in memory.
精彩评论