tweenjs / tween.js

JavaScript/TypeScript animation engine

Home Page:https://tweenjs.github.io/tween.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to update .to value onRepeat?

Turtleted21 opened this issue · comments

Hello

I try to update a value on repeat but .to(value), my ball destination, don't change.
my var "ball_position_target_x" is corretly updated inside .repeat() but not on .to()

const animation_ball = () => {
    var ball_position_target_x = 0;
    
    var ball_tween_1 = new TWEEN.Tween(ball.position)
        .to({ x: ball_position_target_x }, 2000)
        .dynamic(true)
        .easing(TWEEN.Easing.Sinusoidal.InOut)
        .repeat(Infinity)                 
        .onRepeat(() => {
            ball_position_target_x = 50 * (0.5 - Math.random());
            console.log("ball_position_target_x",ball_position_target_x)
        })

    ball_tween_1.startFromCurrentValues();

}
....

function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();
}

Thanks

Hello. You have to tell the tween the new value. Try this (untested):

        .onRepeat(() => {
            ball_position_target_x = 50 * (0.5 - Math.random());
            ball_tween_1.to({ x: ball_position_target_x }, 2000) // <-------- HERE, use the new value.
            console.log("ball_position_target_x",ball_position_target_x)
        })