Jump to content

Using a looping Timer to update a progress bar?


edzillion
 Share

Recommended Posts

Hi

 

Is it possible to use a Phaser.Timer to update a progressbar?

timer1 = game.time.events.loop(Phaser.Timer.SECOND, playerAttack1, this);

I need to be able to find out how far through a loop my timer is - something like (sloppy code but you get what I mean):

var timeleft = timer1.time.next - timer1.time.start;var progressPercent = timeleft / timer1.time.length;

the problem I have is that I am not sure how to get timer1.time.start - I can get the start of the timer, but not the start of the last loop.

 

thanks in advance!

Link to comment
Share on other sites

ok so I got it working using the game.time and timer.next like so:

var n = player.timer1.timer.next;var diff =  Math.abs((n - t)-1000);var perc = (diff / 1000);

where perc is how far through the current loop the timer is (0 -> 1)

:)

 

my next problem is how to deal with multiple timers:

 this.timer1 = game.time.events.loop(Phaser.Timer.SECOND, playerAttack1, this); this.timer2 = game.time.events.loop(Phaser.Timer.SECOND*4, playerAttack2, this);

timer1.next and timer2.next return the same time. the docs say .next returns the time of the next 'event' so I guess that means any event queued in the timer

 

how can I figure that out on a per timer basis?

Link to comment
Share on other sites

ok now im doing this:

  timer = game.time.create(false);    ...    this.timer1 = timer.add(1000, playerAttack1, this);  this.timer1.loop = true;  this.timer2 = timer.add(4000, playerAttack2, this);  this.timer2.loop = true; 

now the percentages look good but my callbacks (playerAttack1 etc) are not being called (?)

Link to comment
Share on other sites

got it. the problem was I needed to start() the timers:

  this.timer1 = timer.add(1000, playerAttack1, this);  this.timer1.loop = true;  this.timer1.timer.start();  this.timer2 = timer.add(4000, playerAttack2, this);  this.timer2.loop = true;    this.timer2.timer.start();

the docs need updating! the examples list about five different ways to do this, none of them current.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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