Binary Moon Posted July 22, 2014 Share Posted July 22, 2014 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 More sharing options...
lewster32 Posted July 22, 2014 Share Posted July 22, 2014 This has been answered here: http://www.html5gamedevs.com/topic/7906-phasertimer-dynamically-change-the-property-delay/ Link to comment Share on other sites More sharing options...
Binary Moon Posted July 22, 2014 Author Share Posted July 22, 2014 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 More sharing options...
lewster32 Posted July 22, 2014 Share Posted July 22, 2014 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 More sharing options...
Binary Moon Posted July 22, 2014 Author Share Posted July 22, 2014 that's awesome - thanks. the tick property worked but created a strange glitch in my clock display so I have gone for the entirely custom clock as you suggested and it's working great. Thanks a lot for your help! lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted July 22, 2014 Share Posted July 22, 2014 No problem - the above is rudimentary and I'm sure you can play with the properties of Phaser's Timers and TimerEvents to get the same functionality, but I thought it quicker to just to suggest that route as it exposes all the workings. Link to comment Share on other sites More sharing options...
Chendler Posted August 26, 2014 Share Posted August 26, 2014 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 More sharing options...
suganya Posted November 25, 2017 Share Posted November 25, 2017 (edited) On 7/22/2014 at 1:25 PM, lewster32 said: This has been answered here: http://www.html5gamedevs.com/topic/7906-phasertimer-dynamically-change-the-property-delay/ On 7/22/2014 at 1:25 PM, lewster32 said: This has been answered here: http://www.html5gamedevs.com/topic/7906-phasertimer-dynamically-change-the-property-delay/ i want to timer,if timer is running extra adding 1 seconds Edited November 25, 2017 by suganya Link to comment Share on other sites More sharing options...
lewster32 Posted November 28, 2017 Share Posted November 28, 2017 It's probably easier to create a custom timer, and then add/remove events to that, taking account of the duration of the timer. Something like this: https://jsfiddle.net/lewster32/8odgdyq6/ Link to comment Share on other sites More sharing options...
samme Posted November 28, 2017 Share Posted November 28, 2017 It looks like you can do timer._start -= 1000; Link to comment Share on other sites More sharing options...
Recommended Posts