Jump to content

Timer onComplete still happening after timer destroyed


Bazgir
 Share

Recommended Posts

Hello,

I'm trying to set up a timer for boss levels, and the boss will reset its health if you don't kill it within the timer period. Unfortunately what's happening is even if you win the level and you move on to the next level or back out and go back to the previous level to grind/farm/whatever, the timer's text will disappear but the function still executes despite the timer being destroyed. I'm not sure if it's a bug with the timer itself, or if I'm just not coding it in properly (most likely).

Below is a snippet of code from the actual kill function. The idea is that you defeat the boss and progress to the next level, the boss timer gets destroyed and only recreated if you get to another boss level. Unfortunately the timer's function (in this case, this.resetMonsterTimer) is still executing regardless of the game.time.events.remove(bossTimer);

if (this.level % 10 == 5 || this.level % 10 == 0) {
            this.levelKillsRequired = 1;
            var bossTimer = game.time.events.add(30000, this.resetMonsterTimer, this, monster);
            var bossTimerSecondsCounter = game.time.events.add(1000, this.lowerTimer, this);
            this.bossTimerSeconds = 30;
            this.bossTimerText.alpha = 1;
            this.bossTimerText.text = this.bossTimerSeconds + 'S';
            } else {
                if (bossTimer) {
                    game.time.events.remove(bossTimer);
                }
                if (bossTimerSecondsCounter) {
                    game.time.events.remove(bossTimerSecondsCounter);
                }
                if (this.bossTimerText.alpha == 1) {
                    this.bossTimerText.alpha = 0;
                }
                this.levelKillsRequired = 10;

 

I've tried using bossTimer.destroy(); instead to no avail. I've also changed between using game.time.events.loop and game.time.events.add (with the following snippet baked into the executed functions to restart the timer) and neither seem to have any different effect.

if (this.level % 10 == 5 || this.level % 10 == 0) {
    var bossTimer = game.time.events.add(30000, this.resetMonsterTimer, this, monster);
}
if (this.level % 10 == 5 || this.level % 10 == 0) {
    var bossTimerSecondsCounter = game.time.events.add(1000, this.lowerTimer, this);
}

 

If anyone has any insight they'd like to offer, I'd appreciate it.

 

Thanks

Link to comment
Share on other sites

bossTimer.destroy() wouldn't work cause  game.time.events.add() returns Phaser.TimerEvent not Phaser.Timer. 

Have you tried adding a simple console.log('test') to the lines where the timer is supposed to be removed?

 

Also since the variables are defined locally in a method , are you sure the method isn't called repeatedly and overwriting  the variables? 

 

Link to comment
Share on other sites

Hello,

Thank you for your replies.

It turns out that the game.time.events.remove were not firing when in the if statements, as found by opening up debugging in chrome and adding breakpoints at those lines. If I comment out the if statements and use the breakpoints, it successfully stops running when it gets to those lines of code. However if I then let it run to the boss level and pass the boss level, the issue where the onComplete is still firing despite having moved on to the next level and the game.time.events.remove supposedly firing.

Adding in console.log("test"); tucked in just below game.time.events.remove(bossTimer); shows the same result.

//if (bossTimer) {
    game.time.events.remove(bossTimer);
//}
//if (bossTimerSecondsCounter) {
    game.time.events.remove(bossTimerSecondsCounter);
//}

I think I've fixed my issue. I've changed them from var bossTimer to this.bossTimer and game.time.events.remove(this.bossTimer); and now the issue isn't happening again.

For the record, the new code looks like this:

if (this.level % 10 == 5 || this.level % 10 == 0) {
    this.levelKillsRequired = 1;
    this.bossTimerSeconds = 30;
    this.bossTimer = game.time.events.add(30000, this.resetMonsterTimer, this, monster);
    this.bossTimerSecondsCounter = game.time.events.add(1000, this.lowerTimer, this);
    this.bossTimerText.alpha = 1;
    this.bossTimerText.text = this.bossTimerSeconds + 'S';
    } else {
       game.time.events.remove(this.bossTimer);
       game.time.events.remove(this.bossTimerSecondsCounter);
       if (this.bossTimerText.alpha == 1) {
           this.bossTimerText.alpha = 0;
       }
    this.levelKillsRequired = 10;
}

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...