简体   繁体   English

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

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

I have set up a match-3 game, and I want to track the number of seconds a user takes to touch valid points (game piece sprites) on the game board: 我已经设置了一个Match-3游戏,并且想跟踪用户触摸游戏板上的有效点(游戏画面精灵)所花费的秒数:
(1) when a game level is loaded, if the user does not touch a sprite within 5 seconds, track this; (1)在加载游戏关卡时,如果用户在5秒钟内未触摸精灵,请进行跟踪;
(2) while playing the game, if the time elapsed between the user touching a sprite is greater than 5 seconds, track this. (2)在玩游戏时,如果用户触摸精灵之间的时间大于5秒,请进行跟踪。
(I will use these results to provide a hint to the user). (我将使用这些结果向用户提供提示)。
I would like to use a NSTimer/NSTimeInterval for this, but not sure how to implement it. 我想为此使用NSTimer / NSTimeInterval,但不确定如何实现它。

I wouldn't suggest adding an integer as others suggested. 我不建议像其他人建议的那样添加一个整数。

All you need is delta time. 您需要的只是时间增量。 I'm assuming you have that part worked out, but I'll post it just in case 我假设您已经解决了这一部分,但我会予以发布,以防万一

scene properties 场景属性

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

your scene's update method (also create an update method for your sprites, and pass delta into it here) 场景的update方法 (也为sprite创建一个update方法,并在此处将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)

your sprite's time properties 你的精灵的时间属性

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

your sprites update / touched method 您的精灵更新/接触方法

func touched(){
    self.timeSinceTouched = 0.0
}

func update(delta: CFTimeInterval) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM