phaserjs / phaser-ce

Phaser CE is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
http://phaser.io
MIT License
1.34k stars 491 forks source link

Multiple timers, but Timer.duration only returns to first one currently running #672

Closed shibekin69 closed 4 years ago

shibekin69 commented 4 years ago

This Issue is about (pick one, ✏️ delete others)

https://codepen.io/shibekin/pen/wvMywzZ https://phaser.io/examples/v2/time/basic-looped-event (modified code based on this example)

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!

shibekin69 commented 4 years ago

Nevermind, I found it:

It'll be something like:

ctimer.tick - ctimer.timer._now = current duration of ctimer in ms

samme commented 4 years ago

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;