addCounter() Parameters and Workarounds

I want to be able to force the counter to return only integers when using getValue, and if possible, to set a step value. Has anyone tried to do this?

It’s trivial to make a counter with a for loop, and you can control some parameters with a library like countUp.js (which let’s you use ease), but for my use case I need the counter to be a tween of a specific length, but with variable values.

The target property doesn’t seem to work for this. If I could target text, then I wouldn’t need getValue. getValue is bad because having to conditionally use toFixed(0) has some side effects.

text = this.add.text(30, 20, '0', { font: '16px Courier', fill: '#00ff00' });

//  A 'Counter' tween is a special type of tween which doesn't have a target.
//  Instead it allows you to tween between 2 numeric values. The default values
//  are 0 to 1, but can be set to anything. You can use the tweened value via
//  `tween.getValue()` for the duration of the tween.

tween = this.tweens.addCounter({
    from: 0,
    to: 100,
    duration: 5000
});

I don’t think so, but you can do

STEP * Math.floor(tween.getValue())
2 Likes

Brilliant, thank you.