Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update .to value onRepeat? #650

Closed
Turtleted21 opened this issue Apr 28, 2023 · 1 comment
Closed

How to update .to value onRepeat? #650

Turtleted21 opened this issue Apr 28, 2023 · 1 comment

Comments

@Turtleted21
Copy link

Turtleted21 commented Apr 28, 2023

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

@trusktr
Copy link
Member

trusktr commented Jun 23, 2023

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)
        })

@trusktr trusktr closed this as completed Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants