سؤال

I am trying to reset my timer after a certain event is triggered. Currently I have my in-game timer which checks for that event every 4 seconds in my update as follows:

timer: new ig.Timer(),
update:function(){
           //console.log(this.timer.delta());
           if (this.timer.delta() >= 0) {
              this.performAchievementChecks();
              this.timer.set(4);
           }
 },

 performAchievementChecks:function(){
           if(tempArray.length >= 1 && this.Achievements[achID].status == this.storage.get(achKey)){
              this.performUpdate(achID);
           } 
 }

  performUpdate:function(achID){
           this.timer.set(0);
           this.updateAchievements(achID);
  }

So my function performAchievementchecks() is called every 4 seconds and within that time if its if statement goes true then I want to reset my timer back to 0 but it does not do it. Could someone please point out what is it that I am doing wrong? If so how do I reset my game timer from other different functions on occurrence of a certain event outside the update()?

هل كانت مفيدة؟

المحلول

I am not sure what do you mean by "reset my timer back to 0", if you only want the timer to reset, you don't have to do anything, as you have already set it this.timer.set(4);, the other option would be to disable it, and for this you can simply set the variable to null.

Tips:

1.You can create the timer directly with target:

timer: new ig.Timer(4)

And then use this.timer.reset(); to restart the timer.

2.You can set the timer variable to null this.timer = null; if you want to disable it, and check if the variable is assigned in update funstion:

if (this.timer && this.timer.delta() >= 0) { ... }

PS: check the impact documentation on timers here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top