【问题标题】:How to implement a NSDate to force users to wait for 'lives' to reload如何实现 NSDate 以强制用户等待“生命”重新加载
【发布时间】:2016-03-04 14:19:44
【问题描述】:

我目前正在编写一个使用生命系统的游戏,用户有 5 条生命,如果用完,则在 20 分钟内无法再次玩(除非他们购买更多积分)。

我想知道如何实现一个计时器来运行 NSDate,基本上是一个后台计时器,它知道事件之间的时间间隔。

我知道用户可以更改日期,所以如果有一种不太复杂的方法来解决它会很有用,但现在任何东西都对我有用。谢谢!

【问题讨论】:

  • 您需要考虑用户在等待时会注销或关闭游戏的事实,比如说,对于这个问题,您需要将原始时间戳 startign 时间存储在服务器上。然后调用一个方法,将服务器上的这个开始时间作为输入,然后根据应用程序中的当前时间检查 20 分钟。我就是这样做的。您可以在用户首次登录或首次创建帐户时使用严格的时区记录来解决此问题,因此您可以根据他们曾经注册的第一个时区来使用此算法,然后在该时区上运行计算跨度>
  • 如果用户更改时区,没关系,您总是在推送时间戳上仅基于该信息提取用户原始时区的信息,或者只是确保所有用户信息都是在格林威治标准时间,这也将起作用。每次打开应用程序时,时间都会重新开始,并使用来自服务器的新输入参数。或者您也可以在 nsuserdefaults 中为其添加时间戳。
  • 所以,你输入服务器上的时间戳与当前时间之间的差异,然后如果这个值超过20分钟,那么用户可以再次播放,如果这个值小于20分钟,从“还剩多少时间”开始计时,免费玩。如果用户关闭应用程序并重新启动,这将是计算应用程序使用之间时间的最简单方法。否则,如果您想变得花哨,请使用流媒体,但这很快就会变得混乱。
  • 你找到解决办法了吗?

标签: ios timer in-app-purchase nsdate clock


【解决方案1】:

我认为您从错误的方向处理任务。

当用户输掉游戏时,您应该保存允许用户再次玩游戏的日期。而且因为您的用户可能会退出您的应用程序,所以您必须将该日期保留在某个地方(例如 NSUserDefaults)。

然后您启动一个计时器,该计时器每秒触发一次,以在用户无法玩游戏时向他显示倒计时。因此,您将保存的日期与现在进行比较(即NSDate())。如果“现在”较晚或与“下一个游戏日期”相同,则允许用户再次玩游戏。如果不是,则更新倒计时标签。

当用户启动应用程序时,您还必须检查用户是否被允许玩。为此,您必须获取持久日期并像以前一样进行比较。

这样的事情会起作用:

import UIKit

// NSDate extension so you can use ==, <= and so on
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
    return lhs === rhs || lhs.compare(rhs) == .OrderedSame
}

public func <(lhs: NSDate, rhs: NSDate) -> Bool {
    return lhs.compare(rhs) == .OrderedAscending
}

extension NSDate: Comparable { }
// NSDate extension end



class YourGame: NSObject {
    var nextGameDate: NSDate?
    var nextGameTimer: NSTimer?

    func userDidLoseGame() {
        // update UI so user cannot play new game ...

        let calendar = NSCalendar.currentCalendar()
        nextGameDate = calendar.dateByAddingUnit(.Minute, value: 20, toDate: NSDate(), options: [])!

        // save date
        NSUserDefaults.standardUserDefaults().setObject(nextGameDate!, forKey: "nextGameDate")

        nextGameTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("refreshNextGameTime:"), userInfo: nil, repeats: true)
        refreshNextGameTime(nextGameTimer!)
    }

    func refreshNextGameTime(timer: NSTimer) {
        if let nextGameDate = nextGameDate where nextGameDate >= NSDate() {
            let formatter = NSDateComponentsFormatter()
            formatter.unitsStyle = .Full
            formatter.allowedUnits = [.Minute, .Second]
            let timeUntilNextGameString = formatter.stringFromDate(NSDate(), toDate: nextGameDate)
            print("Can't play. Next game in \(timeUntilNextGameString)")
            // show user time when he can play again ...
        }
        else {
            timer.invalidate()
            nextGameTimer = nil
            print("Can play")

            // update UI so user can play game ...
        }
    }

    func userDidStartApp() {
        // check if user is allowed to play game
        if let savedNextGameDate = NSUserDefaults.standardUserDefaults().objectForKey("nextGameDate") as? NSDate where savedNextGameDate >= NSDate() {
            // update UI so user cannot play new game ...

            nextGameDate = savedNextGameDate
            nextGameTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("refreshNextGameTime:"), userInfo: nil, repeats: true)
            refreshNextGameTime(nextGameTimer!)
        }
        else {
            // update UI so user can play game ...
        }
    }
}

这是一段代码,它可能包含像 NSCalendar 这样的新概念,但它应该非常简单。如果您不熟悉某些内容,请查看文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2015-05-31
    相关资源
    最近更新 更多