iOS Swift SpriteKit - 如何检测用户触摸精灵之间经过的时间

iOS Swift SpriteKit - how to detect time elapsed between user touches on sprites

我设置了一个三消游戏,我想跟踪用户触摸游戏板上的有效点(游戏块精灵)所花费的秒数:
(1) 当游戏关卡加载时,如果用户在 5 秒内没有触摸精灵,则跟踪此;
(2) 在玩游戏时,如果用户触摸精灵之间的时间间隔大于 5 秒,则跟踪。
(我将使用这些结果向用户提供提示)。
我想为此使用 NSTimer/NSTimeInterval,但不确定如何实现它。

我不建议像其他人建议的那样添加整数。

您只需要增量时间。我假设你已经解决了那部分问题,但我会 post 以防万一

场景属性

// time values
var delta:NSTimeInterval = NSTimeInterval(0)
var last_update_time:NSTimeInterval = NSTimeInterval(0)

您场景的更新方法(也为您的精灵创建一个更新方法,并在此处将delta传递给它)

 func update(currentTime: NSTimeInterval) {
        if self.last_update_time == 0.0 {
            self.delta = 0
        } else {
            self.delta = currentTime - self.last_update_time
        }

        self.yourSprite.update(self.delta)

你的精灵的时间属性

var timeSinceTouched = NSTimeInterval(0)
let timeLimit = NSTimeInterval(5.0)

你的精灵更新/触摸方法

func touched(){
    self.timeSinceTouched = 0.0
}

func update(delta: CFTimeInterval) {

    if self.timeSinceTouched < self.timeLimit {
        self.timeSinceTouched += delta
    } else {
        // five seconds has elapsed
    }