Home button press causes EXC_BAD_ACCESS code=1 in SpriteKit SKView

爱⌒轻易说出口 提交于 2019-12-10 13:28:30

问题


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?


回答1:


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];
}



回答2:


  1. Run Xcode's analyzer (Project > Analyze) to determine if you have memory management issues in your code.

  2. Run this scenario in the allocations instrument of the Instruments app (Project > Profile), which can report any detected misuse of memory.




回答3:


We ran into some similar inconsistent behavior with UICollectionView. In that case, instantiating the object via a Storyboard (versus programmatically) resolved the issue.

On a hunch, I tried that this morning and Viola, success!

So, it appears that Storyboards are doing some magic with SKView on creation that is unknown to us and allows them to be properly terminated on Home Button press. Frustrating, but at least it works now.




回答4:


The problem can be caused by audio, if so you can check the answer here https://stackoverflow.com/a/19283721/1278463

I solved this in a game which is not using audio. Th solution is to pause SKView when entering background:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = YES;
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = NO;
    }
}


来源:https://stackoverflow.com/questions/19058096/home-button-press-causes-exc-bad-access-code-1-in-spritekit-skview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!