tween.js icon indicating copy to clipboard operation
tween.js copied to clipboard

how to use chain when you want to update separate tween animaton?

Open mengxun1437 opened this issue 4 years ago • 4 comments

hello,here is my code

 const tweenA = new Tween(tweenStateInit)
      .to({ x: 100 }, 300)
      .onUpdate(({ x }) => {
        update(x);
      })
      .onComplete(() => {
        console.log('tweenA end');
      });
    const tweenB = new Tween(tweenStateOne)
      .to({ x: 200 }, 300)
      .onUpdate(({ x }) => {
        update(x);
      })
      .onComplete(() => {
        console.log('tweenB end');
      });
    const tween = tweenA.chain(tweenB).start();

    // loop 
    loop(){
      // ...
      tween.update()
    }

I want to use chain to paly two animation. When playing the first, i update the first 'x',when playing the next, it seems lost efficacy. chain returns the first tween object, there is no way to update the another tween?

Hope someone answer,please!

mengxun1437 avatar Aug 02 '22 11:08 mengxun1437

hello,here is my code

 const tweenA = new Tween(tweenStateInit)
      .to({ x: 100 }, 300)
      .onUpdate(({ x }) => {
        update(x);
      })
      .onComplete(() => {
        console.log('tweenA end');
      });
    const tweenB = new Tween(tweenStateOne)
      .to({ x: 200 }, 300)
      .onUpdate(({ x }) => {
        update(x);
      })
      .onComplete(() => {
        console.log('tweenB end');
      });
    const tween = tweenA.chain(tweenB).start();

    // loop 
    loop(){
      // ...
      tween.update()
    }

I want to use chain to paly two animation. When playing the first, i update the first 'x',when playing the next, it seems lost efficacy. chain returns the first tween object, there is no way to update the another tween?

Hope someone answer,please!

it seems that i need to use tweenB.update() in RAF, tween.update() only effect on tweenA The state I want is that as long as you call tween.update, you can be heard by tweenA and tweenB's onUpdate, but now only tweenA's onUpdate executes it

mengxun1437 avatar Aug 02 '22 15:08 mengxun1437

The trick is to add both tweens to a TWEEN.Group then call update on the group:

    tweenA.chain(tweenB).start();

    const group = new Group()
    group.add(tweenA)
    group.add(tweenB)

    // loop 
    loop(){
      // ...
      group.update()
    }

trusktr avatar Aug 04 '22 08:08 trusktr

The trick is to add both tweens to a TWEEN.Group then call update on the group:

    tweenA.chain(tweenB).start();

    const group = new Group()
    group.add(tweenA)
    group.add(tweenB)

    // loop 
    loop(){
      // ...
      group.update()
    }

Thank you very much, i get what you mean,and you can close this issue now!😀

mengxun1437 avatar Aug 04 '22 08:08 mengxun1437

You're welcome! :)

trusktr avatar Aug 04 '22 23:08 trusktr