lucas_turci
lucas_turci

Reputation: 322

Multitask Issue in SpriteKit Game

I'm developing a game with Sprite Kit but I am having a multitasking issue. When I press the home button, during the game execution, my SKScene.paused becomes true, and I make the proper changes to the app in my applicationDidEnterBackground method at AppDelegate.m, such as saving stuff with NSUserDefaults. Anyway, if I opened my app again, it should resume from where it left off, but what happens is that my app terminates and starts again. This only happens in my iPhone (in the iOS Simulator it works fine). Since I am new at creating games with Sprite Kit and creating apps at all, I was hoping for some clue of what could the problem be...

PS: I think the problem is something about the app not being "suspended" correctly, because if I press the home button and immediately reopen the app, it works fine.

Here is my code:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if(self.scene == nil) return;

    [self.scene saveUserDefaults];    
    [self.scene pausar];
    self.scene.paused = true;
}

In MyScene.m, inside the initWithSize method:

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.scene = self;

And in the AppDelegate.h file: @property (weak, nonatomic) MyScene * scene; Now, the saveUserDefaults method, which is inside the MyScene.m file:

-(void) saveUserDefaults
{
    [userDefaults setBool:true forKey:@"active"];
    [userDefaults setInteger:highScore forKey:@"highScore"];
    [userDefaults setBool:soundOn forKey:@"soundOn"];
    [userDefaults setBool:musicOn forKey:@"musicOn"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

Upvotes: 0

Views: 179

Answers (1)

John Vitornio
John Vitornio

Reputation: 71

Well, without seeing more of your code, it is impossible to know what is causing your issue.

First, read this question. Similarly, you should read up on the UIAppDelegate class since it is something you must know very well when programming on a phone where frequent interruptions like phone calls, etc can happen while someone is using your app.

More than likely, you have a problem with how you are responding to one of the protocol methods in the delegate.

Upvotes: 3

Related Questions