Luiz Ribeiro Luiz Ribeiro - 1 year ago 119
Swift Question

Pressing play after returning to App SpriteKit


I'm a very beginner at programming and, while developing my app, an unsolutionable problem showed up. I've tried to pause the game when the user leaves the app itself and, when the return is wanted, the game is pause since de home button is pressed. My difficulty is, when the user leaves the app with the game unpaused and come back, it works perfectly, but when the user pauses the game and leave, Xcode shows me an error message: "Thread 1: signal SIGARBT" and the game crashes. If you take a peek on my game, I'd be very pleased



func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

//pause when leaving app
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseState"), name: "pauseState", object: nil)


import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {

var gameLayer = SKNode()
var pauseLayer = SKNode()

var gameStarted = Bool()

var pauseButton = SKSpriteNode()
var playButton = SKSpriteNode()

func setupPauseButton(){

pauseButton = SKSpriteNode (imageNamed: "pause")
pauseButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
//pauseButton.zPosition = 3
func setupPlayButton(){

playButton = SKSpriteNode (imageNamed: "play")
playButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
//playButton.zPosition = 3

func createGameLayer(){

self.physicsWorld.contactDelegate = self

gameLayer.addChild(pauseButton) //add pauseButton to gameLayer
pauseButton.hidden = true
func createPauseLayer(){

pauseLayer.addChild(playButton) //add playButton to pauseLayer

override func didMoveToView(view: SKView) {
/* Setup your scene here */

//pause when coming back
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseState"), name: "pauseState", object: nil)



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

//run only if game is running
if gameLayer.paused == false{

//pauseButton shows when starting to play
pauseButton.hidden = false

//when touch buttons/screen
for touch in touches{

let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

if node == pauseButton{

else if node == playButton{


if gameStarted == false{ //game didn't start yet

gameStarted = true

Character.physicsBody?.affectedByGravity = true //start falling when touch the screen

//first jump
Character.physicsBody?.velocity = CGVectorMake(0, 0)
Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

Character.physicsBody?.velocity = CGVectorMake(0, 0)
Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

func pauseState(){

pauseButton.hidden = true //hide pauseButton
self.physicsWorld.speed = 0 //pause physics (character, etc)
gameLayer.paused = true //pause gameLayer

self.addChild(pauseLayer) //add pauseLayer
func playState(){

pauseLayer.removeFromParent() //remove pauseLayer

gameLayer.paused = false //unpause gameLayer
self.physicsWorld.speed = 1 //unpause physics (character, etc)
pauseButton.hidden = false //show pauseButton

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */


Answer Source

2 things I noticed with your code

1) As of swift 2.2 apple finally changed the selector methods away from strings. So in your gameScene change the observer to this.

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameScene.pauseState), name: "pauseState", object: nil)

2) In your app delegate you are adding another observer which doesnt make sense. You want to post the notification to the observer you have in GameScene.

Change it to this

NSNotificationCenter.defaultCenter().postNotificationName("pauseState", object: nil)

3) Not sure you are doing this but its always recommended to remove any NSNotification observers when you leave a scene. Call this line


when you transition to a new scene or in GameScenes "WillMoveFromView" method

Also its a good idea if you follow apples naming conventions. Only classes, structs, enums and protocols should start with capital letters. This makes your code more readable for yourself and on SO (your code is marked blue but shouldn't)

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download