Jump to content

Changing timer duration


Binary Moon
 Share

Recommended Posts

I have a countdown timer in my game and I want to increase the duration based upon user activity.

 

The game is a colour matcher and I want to add a second each time the user clears some colours. The theory being that if they're quick enough they can then play the game indefinitely.

 

However I don't know how to add the additional time. Does anyone have any idea?

 

At the moment the code is super simple - I am simply creating the timer with this:

        // timer        this.timer = game.time.create(false);        this.timer.loop(30000, this.updateTimer, this);        this.timer.start();
Link to comment
Share on other sites

Thanks for the link - however I don't think that's what I am looking for.

 

It looks like that is changing the speed of the timer - but I want to be able to add time to the timer.

 

In the example above I set the timer to last 30 seconds - and I'd like to be able to extend that as the user plays. For example when they might get an awards of an extra 10 seconds for doing something awesome halfway through the countdown (at the 21.1 second stage) so I'd like to increase the timer to 31.1 seconds.

 

thinking about it now I guess I could destroy the timer and create a new one that includes the additional time - but that seems a bit messy so I wonder if there's something else.

Link to comment
Share on other sites

Oh I see; in that case I think you can alter the tick property of the TimerEvent (get that via the same method as previously) by adding the time in miliseconds you want to extend it by. You could also do the whole thing manually like this:

var endTime;function startTimer(s) {  // set the end time to the current time + s seconds  endTime = game.time.now + (1000 * s);}function addToTimer(s) {  // add s number of seconds to the time  endTime += (1000 * s)}function removeFromTimer(s) {  // remove s number of seconds from the time  endTime -= (1000 * s)}function stopTimer() {  endTime = 0;}function create() {  startTimer(30);}function update() {  if (endTime > 0) {    // timer is running...    var timeLeftSeconds = Math.ceil((endTime - game.time.now) / 1000);    if (timeLeftSeconds >= 0) {      // ... time hasn't ran out yet, so continue doing your stuff    }    else {      // ... time has ran out, so handle that accordingly here    }  }  else {    // timer is not running...  }}

Rough example: http://jsfiddle.net/lewster32/p4QzF/

Link to comment
Share on other sites

  • 1 month later...

lewster32, I tried to use your example from fiddle.

But it has problem. Look, if we change browser tab - canvas paused, but our timer still working, because we calculate difference between two moments.

 

I really surprised by phaser doesn't have increment/decrement timer functionality.

 

Update

 

Now I'm using custom timer and set property game.stage.disableVisibilityChange to true, but it isn't solution.

I want to stop my game when user changes tab.

Link to comment
Share on other sites

  • 3 years later...
On 7/22/2014 at 1:25 PM, lewster32 said:

 

On 7/22/2014 at 1:25 PM, lewster32 said:

i want to timer,if timer is running  extra adding 1 seconds 

Edited by suganya
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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