"Pausing" the Game in Swift
<p>I created a game in Swift that involves monsters appearing. Monsters appear, and disappear, based on timers using something like this:</p>

<pre><code>func RunAfterDelay(_ delay: TimeInterval, block: @escaping ()-&gt;()) 
{
    let time = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)

    DispatchQueue.main.asyncAfter(deadline: time, execute: block)
}
</code></pre>

<p>and then I'd just call it like this (for example to spawn after 2 seconds):</p>

<pre><code>///Spawn Monster
RunAfterDelay(2) { 
                [unowned self] in
                self.spawnMonster()
 }
</code></pre>

<p>I then do something similar for hiding (after x seconds, I despawn the monster).</p>

<p>So I created a settings icon at the top of the screen, and when you tap it, a giant rectangular window appears to change game settings, but naturally the problem is the monsters still spawn in the background. If I whisk the player away to another screen, I believe i'll lose all my game state and can't come back to it without starting all over (the player might be in the middle of their game).</p>

<p>Is there a way to tell all game timers I've created in the above, i.e.</p>

<pre><code>DispatchQueue.main.asyncAfter(deadline: time, execute: block)
</code></pre>

<p>To pause and resume when I say so? I guess it's fine to do it with all timers (if there isn't a way to label and pause certain timers).</p>

<p>Thanks!</p>