A home button press calls the code EXC_BAD_ACCESS = 1 in SpriteKit SKView

SpriteKit should clear and pause all timers when the home button is pressed.

However, we found that if you pressed the home button while displaying the active SKView, the application will crash. This happens even if this view has already been paused by the user.

It's strange if you press the home button twice and go into multitasking mode, everything will be fine.

Follow Up Note: The simulator works great in both situations, without crashing

Does anyone else see this issue using SpriteKit?

Did you find a reason / solution?

+6
source share
4 answers

I had the same problem, and I decided to manually suspend the root SKView in the ViewController before the application moves 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]; } 
+10
source
  • Run the Xcode analyzer (Project> Analyze) to determine if you have memory management issues in your code.

  • Run this script in the Instruments Application Distribution Tool (Project> Profile), which can report any detected improper memory usage.

0
source

We encountered some similar inconsistent behavior with the UICollectionView. In this case, the problem of creating an object using the storyboard (compared to the software) resolved the problem.

On suspicion, I tried this this morning and Viola, success!

So, it seems that the Storyboards do some magic with SKView when creating that we don’t know, and allows them to finish correctly by clicking the Home Button. Disappointing, but at least it works now.

0
source

The problem may be caused by sound, if so you can check the answer here fooobar.com/questions/218519 / ...

I decided this in a game that does not use sound. Th solution is to pause SKView while 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; } } 
0
source

Source: https://habr.com/ru/post/954809/


All Articles