为什么两个NSTimers冲突?

时间:2015-06-25 15:14:45

标签: ios swift

我有两个计时器:一个每隔0.015秒在屏幕上移动一个图像,一个计算游戏运行时间并显示它的计时器 - 精确到0.1秒:

moveDown = NSTimer.scheduledTimerWithTimeInterval(0.015, target:self, selector: Selector("updatePos"), userInfo: nil, repeats: true)

time = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector: Selector("incrementTime"), userInfo: nil, repeats: true)

我知道这两个功能都能正常工作,但问题是当NSTimer发射时,每0.1秒,图像会重置到原来的位置,并在0.1秒后发送回来之前再次开始移动。增量时间函数非常简单,并且不会以任何方式与其他计时器交互:

func incrementTime() {
    varTime += 0.1
    timeLabel.text = String(varTime)
}

这是updatePos函数:

    func updatePos() {
        ifDone()
        r1.center.y += speed1
        r2.center.y += speed2
        r3.center.y += speed3
        r4.center.y += speed4
        r5.center.y += speed5
        r6.center.y += speed6
        r7.center.y += speed7
        r8.center.y += speed8
        r9.center.y += speed9
        r10.center.y += speed10
        r11.center.y += speed11
        r12.center.y += speed12
        r13.center.y += speed13
        r14.center.y += speed14
        r15.center.y += speed15
        for q in r {
            if (q.center.x > player.center.x - 15) && (q.center.x < player.center.x + 15) && (q.center.y > player.center.y - 15) && (q.center.y < player.center.y + 15) {
                start.text = "RESTART"
                start.center.y = 289
                startOut.center.y = 289
                moveDown.invalidate()
                time.invalidate()
            }
        }
    }

我认为经过的计时器录制时间在某种程度上阻止了其他计时器的工作。我该如何解决这个问题?

我不确定(清楚),但我觉得这可能与他们瞄准“自我”的事实有关。所以当计时器触发它时会干扰moveDown。有什么想法吗?

ifDone():

func ifDone() {
    if (r1.center.y > 540) && (r2.center.y > 540) && (r3.center.y > 540) && (r4.center.y > 540) && (r5.center.y > 540) && (r6.center.y > 540) && (r7.center.y > 540) && (r8.center.y > 540) && (r9.center.y > 540) && (r10.center.y > 540) && (r11.center.y > 540) && (r12.center.y > 540) && (r13.center.y > 540) && (r14.center.y > 540) && (r15.center.y > 540) {

        moveDown.invalidate()
        ad += 0.1
        speed1 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed2 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed3 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed4 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed5 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed6 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed7 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed8 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed9 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed10 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed11 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed12 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed13 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed14 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
        speed15 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))

        for x in r {
            x.center.y = 40
            x.center.x = CGFloat(20+arc4random_uniform(285))
        }


        moveDown = NSTimer.scheduledTimerWithTimeInterval(0.015, target:self, selector: Selector("updatePos"), userInfo: nil, repeats: true)

    }
}

0 个答案:

没有答案