Virtual Pet Games on iPhone
I'm planning to do a virtual pet game with various timers in the game (how often does hunger drop, and the energy system that alot of facebook games use.)
However, I'm thinking that if the user close the app, we won't开发者_开发问答 be able to access the timer of the app right? So is it a must for a server to be available in other to make the app run smoothly.
One solution i thought of was using [NSDate date]
, is it feasible?
Thank you.
A better method is to store the important events, and use a timer in the game strictly to check for the next event. For instance, if the owner feed the dog, just write down that the dog got fed at that real time, and then have your timer fire every so often to see if enough time has passed to warrant making the dog hungry again. That way the actual state of the timer is irrelevant.
Also, this would mean that you don't need more than one timer. You would have a handler method that would look at your state, and trigger other methods as needed based on the amount of time since the last event.
If your pet game requires user to be on the app to play, you could always save the necessary details and reload them back when the user launches the app again. Otherwise, you probably need a server and do synchronization each time.
Computers are really fast these days.
An entire days-worth-of-stats can be generated lickity-split, even without using discrete formulas -- if the app isn't open then the pet doesn't need to "get hungrier" and any stats changes or "daily events" can be generated as soon as the application is active again -- no need to even worry about background events for an inactive application.
However, I suspect that for a more friendly game would use additional "soft" logic to encourage the user to play with the pet without having it starve to death during a business meeting: it's a game and games can make their own rules as long as the user keeps playing.
Happy coding.
It seems to me your question is more about how to get your timers to fire even while the app is closed. The answer is you don't. However, you can use local storage on the iPhone to store the most recent time that event XYZ occurred, and when the app starts up again next (say 1 day later), you can compare the current time with the most recently saved time, and quickly update the state before the user sees anything otherwise. The user will be none the wiser, and it will appear as if time actually elapsed within the game world.
精彩评论