ios - How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed? -


i'm new swift & spritekit , wanted know how possible detect if home button pressed ?

the main problem have nstimer.scheduledtimerwithtimeinterval running generate multiple characters in didmovetoview, , going well, when pause game home button , restart game few seconds later, characters floods out alot. assume nstimer had not been paused , therefore, flooding alot of characters when relaunching app. want know simple solution , example, how pause when home button pressed, , resuming when app relaunches ? love here you!

  testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,             target: self,             selector: "timerupdate",             userinfo: nil,             repeats: true) 

///this part of project code generating characters within didmovetoview

nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("appenterintobackground:"), name:uiapplicationdidenterbackgroundnotification, object: nil)     nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("appbecomeactive:"), name:uiapplicationwillenterforegroundnotification, object: nil)      //start timer , calls timerupdate method every 1.0 seconds       mytimer = nstimer.scheduledtimerwithtimeinterval(1.0,         target: self,         selector: "timerupdate",         userinfo: nil,         repeats: true)    if(skview.level == 3) {      mytimer2 = nstimer.scheduledtimerwithtimeinterval(1.0,         target: self,         selector: "timerupdate",         userinfo: nil,         repeats: true)      }      testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,         target: self,         selector: "timerupdate",         userinfo: nil,         repeats: true)   }  func appenterintobackground(notification : nsnotification) {     let skview = self.view as! gameskview!     print("app in background")     testenemy.invalidate()  //remove timer here when add enter background.     if(skview.level == 3) {     mytimer2.invalidate()     }     mytimer.invalidate() }  func appbecomeactive(notification : nsnotification) {      let skview = self.view as! gameskview!    // print("app active now")     //add timer again when app enter foreground     testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,target: self,selector: "timerupdate",userinfo: nil,repeats: true)      if(skview.level == 3) {          mytimer2 = nstimer.scheduledtimerwithtimeinterval(1.0,             target: self,             selector: "timerupdate",             userinfo: nil,             repeats: true)      }      testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,         target: self,         selector: "timerupdate",         userinfo: nil,         repeats: true)  } 

you can use nsnotificationcenter fire notification when app enter background or foreground shown below code:

var testenemy : nstimer?  override func didmovetoview(view: skview) {     /* setup scene here */     nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("appenterintobackground:"), name:uiapplicationdidenterbackgroundnotification, object: nil)     nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("appbecomeactive:"), name:uiapplicationwillenterforegroundnotification, object: nil)      testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,         target: self,         selector: "timerupdate",         userinfo: nil,         repeats: true) } 

and below helper methods:

func timerupdate() {     print("called") }  func appenterintobackground(notification : nsnotification) {     print("app in background")     testenemy!.invalidate()  //remove timer here when add enter background. }  func appbecomeactive(notification : nsnotification) {      print("app active now")     //add timer again when app enter foreground     testenemy = nstimer.scheduledtimerwithtimeinterval(1.2,target: self,selector: "timerupdate",userinfo: nil,repeats: true)   } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -