ios - SpriteKit - how to correctly pause and resume app -


i have huge issue newest game iphone made games tutorials book's help.

note : method spritekit- right way multitask doesn't work.

so, in viewcontroller.m file, i'm defining private variable skview *_skview.

then, :

- (void)viewdidlayoutsubviews {   [super viewdidlayoutsubviews];    if(!_skview)   {     _skview = [[skview alloc] initwithframe:self.view.bounds];     _skview.showsfps = no;     _skview.showsnodecount = no;     // create , configure scene.     skscene * scene = [mainmenuscene scenewithsize:_skview.bounds.size];     scene.scalemode = skscenescalemodeaspectfill;      // present scene.     [_skview presentscene:scene];     [self.view addsubview:_skview];   } } 

and have _skview defined, , works fine.

but, when interrupt game, resets state initial, so, example, if i'm playing, , calls me, game switches main menu. can't this.

based on site mentioned above, created :

- (void)applicationwillresignactive:(uiapplication *)application {   skview *view = (skview *)self.window.rootviewcontroller.view;   view.paused = yes;  }  - (void)applicationdidbecomeactive:(uiapplication *)application {   skview *view = (skview *)self.window.rootviewcontroller.view;   view.paused = no; } 

but game crashes launched, because second method called , skview* view nil. getting self.window.rootviewcontroller.view doesn't work.

i tried self.window.rootviewcontroller.view.subviews, doesn't work either.

i can't define skview (in viewcontroller.m) :

skview * skview = (skview *)self.view; 

because have errors gamecentercontroller.

can me how correctly actual skview , pause properly??

you subscribe these notifications within viewcontroller manages skview. don't have navigate weird hierarchy obtain it.

[[nsnotificationcenter defaultcenter] addobserver:self                                          selector:@selector(willenterbackground)                                              name:uiapplicationdidenterbackgroundnotification                                            object:nil];  [[nsnotificationcenter defaultcenter] addobserver:self                                          selector:@selector(willenterforeground)                                              name:uiapplicationwillenterforegroundnotification                                            object:nil];  - (void)willenterforeground {     self.skview.paused = no; }  - (void)willenterbackground {     // pause , remember resume animation when enter foreground     self.skview.paused = yes; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -