简体   繁体   English

永久禁用计时器-iOS Swift 4

[英]Permanently disable Timers - iOS Swift 4

I am trying to completely reload a Game Scene after the player failed to achieve the goal after 10 seconds. 玩家未能在10秒后达到目标后,我试图完全重新加载游戏场景。

I use the scheduled Timer function to countdown the seconds. 我使用预定的计时器功能倒数秒。

func runTimer() {
    time.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(GameScene_1.updateTimer)), userInfo: nil, repeats: true)
}

Here is the called function from above: 这是上面的调用函数:

@objc func updateTimer() {
    if time.seconds == 0 {
        //Lost the game - reload
        reloadLevel()

    }else{

        ...
    }
}

When I reload the Game with the following code, the countdown speed gets too high. 当我用以下代码重新加载游戏时,倒计时速度变得太高。

func reloadLevel(){
    //Reset
    time.seconds = 10
    time.timer = nil

    //Reload Level
    let newScene = GameScene_1(fileNamed: "GameScene_1")
    newScene?.scaleMode = self.scaleMode
    let animation = SKTransition.flipVertical(withDuration: 2)
    self.view?.presentScene(newScene!, transition: animation)
}

This runTimer() - function is only called once in my project and this is when the user ends to touch the screen. 这个runTimer()-函数在我的项目中仅被调用一次,这是用户结束触摸屏的时间。 Before this point, everything is fine, but after that, it seems like several timers start to countdown at once. 在此之前,一切都很好,但是在那之后,似乎有几个计时器立即开始倒计时。

Does someone know, how I can reload the scene in a "clean" way, so that nothing from the scene before is saved? 有人知道,我如何以一种“干净”的方式重新加载场景,以便不保存场景中的任何内容吗? ...or how I can permanently stop/delete the previous timer? ...或者如何永久停止/删除上一个计时器?

I tried to write the following three statements at in my opinion relevant places (in the didMove - function, before the scene changes...), to solve this problem, but obviously nothing helped. 我试图在我认为相关的位置(在didMove-函数中,在场景更改之前...)编写以下三个语句来解决此问题,但显然没有任何帮助。

Thank you in advance! 先感谢您!

time.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(GameScene_1.updateTimer)), userInfo: nil, repeats: true)

This creates a retain loop. 这将创建一个保留循环。 The view controller is retaining the timer (via time I assume; it's not exactly clear what time is) and the timer is retaining the view controller (via target: self ). 视图控制器保留计时器(通过我假设的time ;目前尚不清楚确切的time ),而计时器保留计时器(通过target: self )。 Even if that weren't true, the timer is retained also by the run loop, so time.timer = nil doesn't stop it. 即使不正确,计时器也会在运行循环中保留,因此time.timer = nil不会停止它。

It's not clear what time is here, so I'm not precisely certain what the code you need is, but the tool you want is invalidate() . 目前还不清楚什么time是在这里,所以我不正是某些你所需要的代码,但你想要的工具是invalidate() You need to call timer.invalidate() whenever the view leaves the screen ( viewWillDisappear ), and anytime you replace the timer. 每当视图离开屏幕( viewWillDisappear )时,以及每次更换计时器时,都需要调用timer.invalidate() I generally use something like: 我通常使用类似:

var timer: Timer? {
    willSet {
        timer?.invalidate()
    }
}

This way, anytime you assign to timer , the old timer will be invalidated (which is almost always what you meant). 这样,无论何时您将其分配给timer ,旧计时器都会失效(这几乎总是您的意思)。

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

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