Tween Duration Is Set To Zero When Created

If you make a tween and immediately check its duration, it will return 0 regardless of the actual duration value set.

Example here: https://jsbin.com/qegofadogo/edit?html,js,output

Is this intended? I’d like to measure the duration of multiple tweens and trigger an event after the last one is done.

In the example tween.duration is printed before tween creation, you can get the duration in the same function using a timeout for example:

 function create () {

    let testObj = {
        tweenProperty: 0
    }

    tween = this.add.tween({
        targets: testObj,
        tweenProperty: 100,
        duration: 2000,
        onComplete: tweenComplete
    })
    setTimeout(()=>console.log('Tween duration: ' + tween.duration),1)
    
}

If the duration was printed before the tween was actually created, then wouldn’t you get an error or undefined rather than printing a zero? If I move the console.log line above the tween creation I get ‘tween is not defined’.

I think only the keys of the Tween object are passed on declaration, and the values are passed once the tween starts.

function create () {

    let testObj = {
        tweenProperty: 0
    }

    tween = this.add.tween({
        targets: testObj,
        tweenProperty: 100,
        duration: 2000,
        onComplete: tweenComplete,
        onStart:()=>console.log(tween.duration)
    })
    
}

The same thing happens if you make a tween manually with the Phaser.Tweens.Tween() class. I wonder if there’s a reason it’s done this way in Phaser. I’ll just find a workaround in the meantime.

I would use a timeline for that, or else several tween onComplete callbacks.

Tween durations are calculated when the tween is started.