ios - stop/start NSTimer when app goes to background -


as title suggests, trying stop nstimer when game goes background , start when game starts again. interruptions such receiving phone call etc..

i using following code in app delegate stop , restart scene when app gets interrupted, not stop timer.

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

in scene place timer, have placed observers in -(id)initwithsize:(cgsize)size method. here observers:

[[nsnotificationcenter defaultcenter]          addobserver:self          selector:@selector(gobackground)          name:uiapplicationdidenterbackgroundnotification          object:nil];  [[nsnotificationcenter defaultcenter]          addobserver:self          selector:@selector(backbackground)          name:uiapplicationdidbecomeactivenotification object:nil]; 

and selector methods:

- (void) gobackground {     [timer invalidate], timer = nil; }  - (void) backbackground {     if([timer isvalid]) {         [timer invalidate];     }     timer = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(updatecounter:) userinfo:nil repeats:yes];  } 

and trying deallocating observers follows:

- (void)dealloc {     [[nsnotificationcenter defaultcenter] removeobserver:self name:uiapplicationwillresignactivenotification object:nil];     [[nsnotificationcenter defaultcenter] removeobserver:self name:uiapplicationdidbecomeactivenotification object:nil]; } 

the code above gets fired, have placed nslog on above methods. happens is, works scene 1 place level 1 , when player wins level 1 , goes level 2 , recreate app interruption scenario again, comes , works few seconds before game crashes.

obviously code not working , have tried every way know how around that, no avail. timer have show how time left in level , has nothing win/lose scenario.(this maybe bit of info may not needed know).

is there anyway can stop , restart timer when game goes background? appreciate help.

i don't know if timer code needed follows:

- (void) gametimerlabel {     gametimerlabel = [sklabelnode labelnodewithfontnamed:@"arialroundedmtbold"];    // gametimerlabel.text = [nsstring stringwithformat:@"%d", _lives];     gametimerlabel.fontsize = 20.0;     gametimerlabel.fontcolor = [skcolor colorwithred:135.0/255 green:207.0/255 blue:232.0/255 alpha:0.6];     gametimerlabel.position = gametimerlabel.position = cgpointmake(270, 282);     [self addchild:gametimerlabel]; }  - (void)updatecounter:(nstimer *)thetimer {     if(secondsleft > 0 ){         secondsleft -- ;         //hours = secondsleft / 3600;         minutes = (secondsleft % 3600) / 60;         seconds = (secondsleft %3600) % 60;         gametimerlabel.text = [nsstring stringwithformat:@"%02d:%02d", minutes, seconds];     }     else{         secondsleft = 70;     } }  -(void)countdowntimer{      secondsleft = hours = minutes = seconds = 0;     if([timer isvalid]) {         [timer invalidate];     }     timer = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(updatecounter:) userinfo:nil repeats:yes];  } 

it count down timer nothing fancy.

edit:

ben-g, right. there no way manage nstimer in background/foreground, used skaction approach , did job done. interested, here working code:

@implementation myscene { sklabelnode *gametimerlabel; int countdownint;     int secondsleft; }  -(id)initwithsize:(cgsize)size {     if (self = [super initwithsize:size]) { secondsleft = 70; [self gametimerlabel]; }     return self; }  - (void) gametimerlabel {     gametimerlabel = [sklabelnode labelnodewithfontnamed:@"arialroundedmtbold"];     gametimerlabel.fontsize = 20.0;     gametimerlabel.fontcolor = [skcolor colorwithred:135.0/255 green:207.0/255 blue:232.0/255 alpha:0.6];     gametimerlabel.position = gametimerlabel.position = cgpointmake(270, 282);     [self addchild:gametimerlabel];     countdownint = secondsleft;     skaction *updatelabel = [skaction runblock:^{         gametimerlabel.text = [nsstring stringwithformat:@" %02d", countdownint];         --countdownint;     }];     skaction *wait = [skaction waitforduration:1.0];     skaction *updatelabelandwait = [skaction sequence:@[updatelabel, wait]];     [self runaction:[skaction repeataction:updatelabelandwait count:secondsleft] completion:^{         gametimerlabel.text = @"time's up";     }]; } 

thats all. again ben-g pointing me in right direction.

even though doesn't answer question exactly, refer accepted answer of question: spritekit - creating timer.

in general want avoid using nstimer when working game engine. timed actions not integrated game loop , timers won't stopped automatically when game paused.

instead integrate update method or use skaction.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -