Phil
Phil

Reputation: 3045

How can I see that a real world time has passed from a stored start date in obj-c/ios?

In my game players will be able to plant a tree, which will take 3 hours to grow. When the player logs back in how would I calculate whether that tree is now fully grown? the creation date of the tree (planted) will be stored on a server, and I will be storing the 3 hours in seconds (not sure if that should be an int of NSNumber) also on the server.

Thanks for any help.

Upvotes: 0

Views: 63

Answers (1)

Joe Hankin
Joe Hankin

Reputation: 960

Get your creation date back from the server as an NSDate, and use timeIntervalSinceDate: like so:

NSDate *now = [NSDate date];
NSTimeInterval secondsSincePlanting = [now timeIntervalSinceDate:plantingDate];
if (secondsSincePlanting > secondsRequiredForTreeToFullyGrow) {
   ...
}

Upvotes: 1

Related Questions