I tried to search everywhere in the code:Explained documentry what it does when going to background, or if it is even paused sometime, but to no avail- can someone direct me in the way of what is recommended to do when going to background in sprite kit enabled game?
Should I just call scene.paused = YES
, or how can I confirm that no drawing occurs in background so I can avoid termination by iOS which won't allow me that?
Thanks!
I was wondering why in the world this seemingly simple solution was causing me crashes on app startup, but it was because I had iAd running. So, a thing to keep in mind: @MassivePenguin's answer works but if you have iAd going you will have to grab the
SKView
via thesubviews
or it (obviously) crashes.For me did not work none of one provided solutions. I did found another way how to do it.
SpriteKit really isn't documented that well yet, probably because it's so new that relatively few developers have implemented it. SpriteKit should pause animations when backgrounding, but in my experience (and harrym17's) it will cause memory access errors and quits the app entirely rather than backgrounding it.
According to the Apple Developer forums, it seems this is a known bug in SpriteKit (it doesn't always pause animations when backgrounding, as it should) causing memory access errors as per harrym17's comment. Here's a brief fix which has worked for me - my app was consistently crashing when backgrounding, and since adding this code everything works fine.
(Kudos to Keith Murray for this code on the Apple forums; as far as I can see he hasn't posted it on SO).
In
AppDelegate.h
, make sure you import SpriteKit:In
AppDelegate.m
, edit yourapplicationWillResignActive
method:And this to your
applicationDidBecomeActive
method:There are additional problems when backgrounding when playing audio via
AVAudioSession
, apparently; a workaround is mentioned in this thread.Sprite Kit automatically pauses its animation timers when moving to the background -- you don't need to worry about your app getting killed for that.
iPad seems to background ok, but iPhone definitely crashes when backgrounding a sprite kit game. You have to do this manually, the automagical piece isn't so automagical.
As said here by
LearnCocos2D
: