Jump to content

Timer loop that slowly gets faster


Martti Laine
 Share

Recommended Posts

Is it possible to update the timing between a loop after it has been initialized? I'd like to slowly speed up the loop while time goes on.

 

Something like this is what I'm after:

// score.hasPassed is not a real method, but it doesn't matter hereif(score.hasPassed(500)) {    obstacleTimer.updateDelay(Phaser.Timer.SECOND / 2);}

That is obviously not functional.

Link to comment
Share on other sites

I was curious about that the other day and looked into it a little further. I don't know if this is the official way to handle it, but you can change the delay property on the timer itself. Here is some code that demonstrates it. You can click on game to speed up the loop.

 

View the Demo

var game = new Phaser.Game( 200, 200, Phaser.CANVAS, '', { create: create,render: render } );function create() {  this.obstacleTimer = game.time.events.loop( 1000, timerCallback, this );    game.input.onDown.add( speedUp, this );}function render() {  game.debug.text( this.obstacleTimer.delay, 100, ( this.obstacleTimer.timer.duration / this.obstacleTimer.delay ) * game.world.height );}function timerCallback() {  console.log( this.obstacleTimer.delay );}function speedUp() {  this.obstacleTimer.delay -= 100;}

You'll get a visual cue on the game and in the console that the timer is speeding up as you click. Let me know if that was what you meant.

 

I've noticed an issue with this though. When you change the delay, you still have to wait for the end up the current loop delay to finish up, then it will take on then new value. This could be an issue if you need your delay to change immediately. I am sure there is a way to force the current loop delay to auto complete immediately, but I couldn't find it in the docs. If anyone has a solution for that problem, please share it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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