Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Home button press causes EXC_BAD_ACCESS code=1 in SpriteKit SKView

SpriteKit is supposed to clean up and pause all timers when you press the home button.

However, we have found that if you single tap the home button while displaying an active SKView, the app crashes. This occurs even if that view is already paused by the user.

Strangely, if you double tap the home button and move to the multitasking view, everything works fine.

Follow Up Note: The simulator works perfectly in both situations, no crash

Is anyone else seeing this issue with SpriteKit?

Have you found the cause / solution?

like image 522
MobileVet Avatar asked Sep 27 '13 18:09

MobileVet


1 Answers

I was having the same problem and I solved it manually pausing the root SKView in the ViewController before the app moving to the background:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view (already setup as SKView via storyboard)
    SKView * skView = (SKView *)self.view; 

    // Create and configure the scene.
    SKScene *scene = [Menu sceneWithSize:CGSizeMake(skView.bounds.size.height, skView.bounds.size.width)];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}

- (void)appWillEnterBackground
{
    SKView *skView = (SKView *)self.view;
    skView.paused = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterForeground)
    name:UIApplicationWillEnterForegroundNotification
    object:NULL];
}

- (void)appWillEnterForeground
{
    SKView * skView = (SKView *)self.view;
    skView.paused = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}
like image 142
tanzolone Avatar answered Nov 07 '22 20:11

tanzolone