Jump to content

Adding tween targets to existing tween


Doug
 Share

Recommended Posts

Hi All

I'm writing a basic platformer which has prizes that I'd like to be undulating in scale.  The prizes are generated dynamically as the game progresses.  I am using the following tween for this, newly assigning the tween to each new prize as it is created:

this.tweens.add({
   targets: [scObjCreated],
   scaleX: '-=.2',
   scaleY: '-=.2',
   duration: 700,
   ease: 'Sine.easeInOut',
   repeat: -1,
   yoyo: true
});

However, this seems inefficient as I know that you can have one tween with multiple targets, so having loads of tweens that all do the same thing seems like poor practice.

I tried assigning the tween to a variable as follows:

this.MyTween = this.tweens.add({
   targets: [scObjCreated],
   scaleX: '-=.2',
   scaleY: '-=.2',
   duration: 700,
   ease: 'Sine.easeInOut',
   repeat: -1,
   yoyo: true
});

...and then pushing the newly created prize objects to the "targets" array as follows:

this.MyTween.targets.push(myNewObject);

However, this doesn't seem to do anything.

Is there a way to do this please?

Thank you!

Link to comment
Share on other sites

Thanks both.  @samme that's a nice example.  Is there a way to actually add tween targets on the fly though?  So create the tween and then add tween targets to that tween as the game progresses?

Link to comment
Share on other sites

Thanks.  This is more the kind of thing I was after, much appreciated. 

However, it seems be replacing the current target with the new one and then restarting rather than adding new targets to the existing tween.

I assume that you can't just push to the targets array without restarting?

Link to comment
Share on other sites

Oh, I think I see now.

If you're tweening in the same range (start–end) for all targets, you can just tween one dummy object and copy its values to the rest.

But personally I would just use multiple tweens. They're meant to be disposable. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...