Doug

Adding tween targets to existing tween

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!

Share this post


Link to post
Share on other sites

You could re-target tweens by editing the tweenData directly:

http://jsfiddle.net/0rg0tcw8/4/

Adding targets should be possible too by fiddling with the tweenData, but I think you have to manually keep track of tween.totalData (the length of the tween data array) , since totalData is cached when adding A tween.

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.