Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从gameviewcontroller调用时,Timer.invalidate()在swift中不起作用_Ios_Swift_Timer_Viewcontroller_Invalidation - Fatal编程技术网

Ios 从gameviewcontroller调用时,Timer.invalidate()在swift中不起作用

Ios 从gameviewcontroller调用时,Timer.invalidate()在swift中不起作用,ios,swift,timer,viewcontroller,invalidation,Ios,Swift,Timer,Viewcontroller,Invalidation,我在使代码中的计时器无效时遇到问题。 我是这样宣布的: var gameTimer: Timer! func pause(){ if gameTimer != nil { gameTimer.invalidate() gameTimer = nil } } self.view.window?.rootViewController?.dismiss(animated: true, comp

我在使代码中的计时器无效时遇到问题。 我是这样宣布的:

var gameTimer: Timer!
func pause(){
          if gameTimer != nil {
              gameTimer.invalidate()
              gameTimer = nil
          } 
    }
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

        if let scene = GameScene(size: view.frame.size) as? GameScene {
            scene.pause()
        }
在GameView中,并在viewDidLoad()中使用它:

我在GameView中有另一个使计时器无效的函数,它如下所示:

var gameTimer: Timer!
func pause(){
          if gameTimer != nil {
              gameTimer.invalidate()
              gameTimer = nil
          } 
    }
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

        if let scene = GameScene(size: view.frame.size) as? GameScene {
            scene.pause()
        }
上面的函数和我在GameView中测试的一样好。但是,当我在GameViewController中这样调用它时:

var gameTimer: Timer!
func pause(){
          if gameTimer != nil {
              gameTimer.invalidate()
              gameTimer = nil
          } 
    }
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

        if let scene = GameScene(size: view.frame.size) as? GameScene {
            scene.pause()
        }
视图切换,这正是我想要的,但时间不会像我想要的那样停止。 有人知道为什么吗

class GameViewController: UIViewController, UIViewControllerTransitioningDelegate {

    var pauseButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        pauseButton.addTarget(self, action: #selector(goToMenu), for: .touchUpInside)
        self.view.addSubview(pauseButton)


        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the scene
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }

    }


    func goToMenu(){
        self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

        if let scene = GameScene(size: view.frame.size) as? GameScene {
            scene.pause()
        }
    }

您正在创建一个新的
游戏场景实例
,并在其中调用
暂停
。你需要在你已有的实例上调用
pause
。哦,对了,我没想到。谢谢隐马尔可夫模型。。。那么你知道我应该如何在GameViewController中调用这个游戏场景函数吗?我到处找了,这是我找到的唯一方法,否则就不行了。我实际上是想在切换到SecondViewController时暂停游戏场景,现在唯一运行的是计时器。谢谢当您创建第一个游戏场景实例时,您需要在属性变量中保留对它的引用。您会怎么做,因为无论我在GameViewController中声明GameSecene的什么地方,我都不能说GameSecene.pause()。我知道还有另一种编写GameSecene.isPaused=true的方法,但这仍然不起作用。您在视图控制器中的何处创建游戏场景?显示该代码