Deeyennay
Deeyennay

Reputation: 83

iOS Swift - How do I respond to a banner notification?

My Sprite Kit game has trouble reacting to banner notifications. When the user gets an alert notification, the game pauses as intended thanks to applicationWillResignActive, which isn't called with banner notifications. When there is a banner notification, the projectiles in the game go straight through walls instead of being removed from their parent after making contact with the (thick) wall.

I've searched, but I can't find any function that I can use to pause the game when the user gets a banner notification. Anyone know of a function I can use?

Upvotes: 1

Views: 420

Answers (1)

crashoverride777
crashoverride777

Reputation: 10664

You have 2 options basically

Pause the SKView in your ViewController

self.view?.paused = true

The problem here is that it will pause everything which makes it a bit trickier to add pauseMenus or other nodes after.

The better way is to create a worldNode (global)

let worldNode = SKNode()

add it to the scene

addChild(worldNode)

than add all your sprites that you need paused to that node

 worldNode.addChild(yourSprite)

and than finally when you want to pause the game you call

worldNode.paused = true
self.physicsWorld.speed = 0 // call this as well when paused and reset to 1 when resuming

This way you can still add pauseMenu nodes/sprites, labels and so on while your game is paused.

To do this when a notification is received I believe you need to use the delegates methods in a your app delegate such as this one

application:didReceiveLocalNotification

You can read more here

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:

Upvotes: 3

Related Questions