Game Exits from Pause State After Resuming It from Background in Swift

Keeping the game paused after app become active?

I think a better way is instead of pausing the whole scene you could create a worldNode in your GameScene and add all the sprites that need to be paused to that worldNode. Its better because if you pause the scene you cannot add pause menu nodes or use touches began etc. It basically gives you more flexibility pausing a node rather than the whole scene.

First create the world node (make global property if needed)

 let worldNode = SKNode()
addChild(worldNode)

Than add all the sprites you need paused to the worldNode

 worldNode.addChild(sprite1)
worldNode.addChild(sprite2)

Create an enum for your different game states

enum GameState {
case Playing
case Paused
case GameOver
static var current = GameState.Playing
}

Than make a pause func in your game scene

 func pause() {
GameState.current = .Paused
//self.physicsWorld.speed = 0 // in update
//worldNode.paused = true // in update

// show pause menu etc
}

And call it like you did above using NSNotification or even better using delegation.

I prefer this method way more than pausing the scene from the gameViewController and also pausing the whole scene.

Create a resume method

 func resume() {
GameState.current = .Playing
self.physicsWorld.speed = 1
worldNode.paused = false

// remove pause menu etc
}

and finally add this to your update method

override func update(currentTime: CFTimeInterval) {

if GameState.current == .Paused {
self.physicsWorld.speed = 0
worldNode.paused = true
}

Spritekit sometimes tends to resume the game when the app becomes active again or when an alert such as for in app purchases is dismissed. To avoid this I always put the code to actually pause the game in the update method.

Hope this helps.

Trying to Pause Timers and resume them

Date is a class, Date() returns an instance of Date initialised to "now", you want:

let elapsedTime = timeCaptured.timeIntervalSince(Date())

Unable to pause Game Scene when home button pressed

You can set an observer for UIApplication.willResignActiveNotification like this, without using your custom notification:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)

@objc func appMovedToBackground {
// You will need to pause the game here, save state etc.
// e.g. set SKView.isPaused = true
}

This page from Apple contains more information. It states that SKView.isPaused should be set automatically when an app is sent to the background.

One point to consider. If the nodes’ movement is based on a timer relative to an absolute point in time, this would have the effect of updating the positions as if they had been moving while in the background.

Finally, are you calling isPaused on the SKScene?

Swift GameplayKit pause GKAgent without pausing scene

I think I found a solution now that seems to work.

Firstly I am stoping the udapteDeltaMethods when the game is paused.

I than look through all my entities and set the agent delegate to nil

  for entity in baseScene.entityManager.entities {
if let soldier = entity as? BossWorld3Soldiers {
soldier.agentComponent.delegate = nil
}
}

Than when I resume my game I call this

  for entity in baseScene.entityManager.entities {
if let soldier = entity as? BossWorld3Soldiers {
let action1 = SKAction.waitForDuration(0.5)
let action2 = SKAction.runBlock({ soldier.resetAgentDelegate() })
baseScene.runAction(SKAction.sequence([action1, action2]))
}
}

The resetAgentDelegate() method is just a convenience method in my entities classes to reset the agent delegate

 func resetAgentDelegate() {
self.agentComponent.delegate = self
}

I am using a slight delay upon resume before resetting the agent delegate because without the delay the enties/agents seem to make a massive jump/dissappear for a few seconds before resuming their GKGoals.



Related Topics



Leave a reply



Submit