Multiple timers, but Timer.duration only returns to first one currently running
shibekin69 opened this issue · comments
This Issue is about (pick one, ✏️ delete others)
- A bug in the API:
- Phaser version(s): 2.11e
- Live example:
https://codepen.io/shibekin/pen/wvMywzZ
https://phaser.io/examples/v2/time/basic-looped-event (modified code based on this example)
- What steps produce the bug: Please see below.
- What should happen: I'm expecting a specific eventTimer's duration.
- What happens instead: I'm getting the first eventTimer's duration that was fired. If the first timer is stopped or destroyed, it will then return the duration of the second eventTimer that was fired.
Hi,
I'm not sure if this is a bug really, but I'm trying to get a timer's duration. The example only shows 1 loop timer event being fired, so it's not a problem in that case, but if I'm to use multiple timers, by doing something like:
atimer = this.game.time.events.loop(1000, function(), this);
btimer = this.game.time.events.loop(5000, function(), this);
ctimer = this.game.time.events.loop(10000, function(), this);
by doing btimer.timer.duration or ctimer.timer.duration on the console, I'm only getting the value from atimer.timer.duration. If by any chance atimer is destroyed, I will now see btimer.timer.duration's value.
Is there a way to get the duration of a specific timer that's running? We can see that in the example, the second loop is counting accordingly, but I don't know how to get the duration of that timer.
Thanks!
Nevermind, I found it:
It'll be something like:
ctimer.tick - ctimer.timer._now = current duration of ctimer in ms
Those are three TimerEvents added to one Timer, so they are all reading the same duration.
I suppose we could add a new method, Timer#getDuration(timerEvent)
.
You could add separate timers instead, e.g.,
var aTimer = this.time.create();
var aEvent = aTimer.loop(/*…*/);
// …
aTimer.duration;
aTimer.ms;
aTimer.seconds;