How to Update Tween Properties?

I want to change the duration randomly just before I play a tween. How can I update a specific tween’s properties?

var tween = this.tweens.create({ targets: sprite, x: 50, y: 50, duration: getDuration(100,500), ease: 'Back', easeParams: [.5], repeat: 0 })

function playTween() {
tween.updateTo('duration', getDuration(100,500)) //<= This doesn't work.
tween.play()
}

function getDuration(min, max) {
    return Math.floor(Math.random() * (max - min)) + min
}

The duration of a tween can be a function (instead of a number) which will be called every time the tween is initialized. Try using duration: getDuration.bind(null, 100, 500) in the tween config (bind creates a function with pre-filled arguments).

updateTo affects the properties of the object affected by the tween, not the tween itself.

2 Likes

Perfect, something I did not consider.

What does the null argument do? Is that a callback?

The one to bind? It’s the value that this will have inside the function. I’ve left it as null because it’s not used in getDuration.

1 Like