4

I have an iOS SpriteKit game, which has a moving node. When you press the pause button, the node.isPaused = true line gets called. But if you press pause, then minimize the app, and relaunch it, the node gets back to moving, even under the transparent "Pause" menu.

I have tried the proposition from the following answer: Answer1. But, even if I call my scene's pauseGame method in any state, the function gets called, but the node restarts moving even under the pause menu.

I have also tried the answer from: Answer2. But the CBApplicationDidBecomeActive() isn't even getting called.

And I have even tried using scene?.view?.isPaused = true in the methods of link 1, but it doesn't work.

Please help.

I know there are lots of similar questions out there, but none of them seem to help solve what I have. (and even, the question from the 2nd link didn't even get an accepted answer)

Fayyouz
  • 682
  • 7
  • 18
  • What swift are you using – Knight0fDragon Jul 20 '17 at 13:34
  • Generally, you becauae you want your game to stay paused, to pause your it when app become active because the game (current scene) is unpaused automatically at that point...Take a look at this https://stackoverflow.com/a/44855288 – Whirlwind Jul 20 '17 at 21:19
  • @Whirlwind isn't there a way To keep it paused?!?! All games stay paused after reactivating – Fayyouz Jul 21 '17 at 11:42
  • @Knight0fDragon Swift 3 – Fayyouz Jul 21 '17 at 11:43
  • @Fayyouz There is a way, like a said - you pause it manually in didBecomeActive (eg. post notification, and listen for it in game scene). Before iOS 9, iirc, the game was automatically paused after returning from the background. But currently, things works like this (the game is automatically unpaused). I don't see a problem with this, it is just a bit more coding eg few lines . – Whirlwind Jul 21 '17 at 11:44
  • @Whirlwind There is a problem with pausing the scene all together. If I use `pauseGame(); self.isPaused =true` inside `appDidBecomeActive`, it pauses all the scene and doesn't call the _paused UI_. If I use only `pauseGame()`, the _paused UI_ appears, but I can see the nodes moving under it (cz its transparent), which means that the `appDidBecomeActive()` unpauses all nodes after being called. – Fayyouz Jul 21 '17 at 11:59
  • First of all, what is `pauseGame()` ;) Also, inside appDidBecomeActive, self is not referring to the scene. I can't fully understand your comment. But say that you have a scene, and a spinning node in it. When your app goes background, the node (the scene actually) will be paused. When you return from background, it will be unpaused so you will be seeing it spinning. This is normal behaviour. So, like I said, you post a notification from AppDelegate's didBecomeActive method, listen for it in your scene. When a notification comes, you pause your scene, so everything stay paused. – Whirlwind Jul 21 '17 at 12:06
  • If you can, post an example project of your problem on GitHub, and I will explain where the error is, and how to solve it. Keep in mind that when you pause the scene, you can't really run actions on it, or something like that. So for example, if you want to show a pause menu before you pause the game, the you 1. post a notification 2. when notification arrives, show pause menu, 3. pause the game. And that's it. You can't pause the game first, and then show the menu because the scene is paused... – Whirlwind Jul 21 '17 at 12:07
  • @Whirlwind okay i'll post on GitHub – Fayyouz Jul 21 '17 at 12:10
  • @Fayyouz Okay, I have few minutes more, so if you manage to post it I'll look at it right now. If not, I will come back at you in a few hours. – Whirlwind Jul 21 '17 at 12:25
  • @Whirlwind It's not a problem, i can wait. Here's the link for it: https://github.com/fayez98hellani/SampleForHelp – Fayyouz Jul 21 '17 at 13:27

1 Answers1

3

Change pauseGame() and resumeGame() methods to look like this:

private func pauseGame() {

        node.speed = 0.0
        gameState = .paused
        pauseLayer.run(SKAction.fadeAlpha(to: 0.5, duration: 1))


 }
 private func resumeGame() {
        pauseLayer.run(.fadeOut(withDuration: 1))
        node.speed = 1.0
        gameState = .inProgress
 }

Note that I am using speed property instead of isPaused property... That is all the difference you need to make. Can't really say why this happens because this behaviour is not documented, but I have seen it already, and been writing about it probably more in detail in some of my previous posts.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • ugh this should not be way way to do things lol, (not blaming you, blaming Apple) For other people seeing this, `node` and `pauseLayer` are 2 separate nodes that are children of the scene. This code will stop any children nodes of `node` from doing actions of any kind, but if you have physics moving your sprites, it won't stop the physics from moving. – Knight0fDragon Jul 21 '17 at 17:41
  • @Whirlwind Thanks a lot! It finally worked, and took just a minute to implement in the original app! – Fayyouz Jul 21 '17 at 18:54
  • @Fayyouz Glad to hear you fixed it! :) – Whirlwind Jul 21 '17 at 19:58
  • @Knight0fDragon Check this out : https://stackoverflow.com/a/34593405 Note the OP's comment where he says that both methods worked. I can't really remeber when I switched to speed property, but that happened because isPaused didnt work for me few times... – Whirlwind Jul 21 '17 at 20:00
  • @Whirlwind this is about actions, not the nodes themselves – Knight0fDragon Jul 21 '17 at 20:02
  • @Whirlwind, no the other SO question you told me to look at, he wanted to pause actions so you were changing the speed on an action to pause it – Knight0fDragon Jul 21 '17 at 20:05
  • I wonder if the update function is still running on your scene if you do this to the scene – Knight0fDragon Jul 21 '17 at 20:07
  • If you pause the scene, it will stay paused. @Knight0fDragon Unlike the specific nodes – Whirlwind Jul 21 '17 at 20:33
  • @Whirlwind you are not pausing the scene, you are dropping the speed to 0. I am wondering if you drop the scene speed to 0, does update still get called – Knight0fDragon Jul 21 '17 at 20:34
  • @Knight0fDragon Just checked ... `update:` is called even if scene's speed is equal to 0.0 (unlike when `scene.isPaused = true`). – Whirlwind Jul 21 '17 at 22:05