Jump to content

Restart tween


tips4design
 Share

Recommended Posts

I am trying to re-use an already created tween. I tried changing tween.target to my new sprite and call tween.start() but it doesn't start. Can I do something like tween.restart(); or do I have to look through the properties like hasStarted and also reset those? (so TweenData isn't computed again).

 

I've noticed that TweenData is computed in the start function (or is it??). So, how do I start again from the first timeline?

Link to comment
Share on other sites

If you call stop() on tween and want to reuse it with start() again, then you have to manually set property pendingDelete to false.

 

Reassigning tween to another target can work, but I thing you are pushing it little to extreme here. You can get into problems if doing this for example with yoyo tweens. I debugged Phaser tweens long time ago to see how it works and as tweens have many options, there are some internal variables (like pendingDelete) to keep track what is going on. I think, it is OK to reuse tween on single target with that pendingDelete trick, but I also think there is no big performance/memory saving from reusing single tween for multiple targets.

Link to comment
Share on other sites

Tweens are not as re-usable as you might expect.  As Tom said, pendingDelete=false, and also I've been doing timeline = [], it's a bit bodgy but works for me and the simplistic way I use them, YMMV.

 

There is a measurable performance cost in creating new tweens, especially large batches of them, which is why re-use is attractive.

Link to comment
Share on other sites

Tweens are not as re-usable as you might expect.  As Tom said, pendingDelete=false, and also I've been doing timeline = [], it's a bit bodgy but works for me and the simplistic way I use them, YMMV.

 

There is a measurable performance cost in creating new tweens, especially large batches of them, which is why re-use is attractive.

Why wouldn't them be re-usable? I can re-use them if the tween has ended by itself, look at the posted github issue. I can not re-use them if I stop them during play and then try to start them again.

 

I don't want timeline = [], because that's the point: re-use the already create timeline, don't compute another identical one.

Link to comment
Share on other sites

In your non-working example (http://jsfiddle.net/5qzjjegt/13/), change it like this:

function scale() {    scaleTween.stop();        scaleTween.pendingDelete = false;    // <==        i = (i + 1) % 2; // If this is commented it works on one sprite    sprites[i].width = 1;    sprites[i].height = 1;        scaleTween.target = sprites[i];    scaleTween.start();}

... and it works. But from my experience: it is ok for simple tween. There are problems at least when tween is yoyo one and there will be probably other problems under specific circumstances.

 

As Rich answered you on GitHub: "Stopping a Tween is the same thing as killing it.". Here you are killing it and then saying: "no, no, it is not dead" ... but ... you are risking that it became kind of zombie inside :)

Link to comment
Share on other sites

This seems like a hacky way to solve it, but it's working. The tween I used is a simple scaleTween (+position tween) like the one in the fiddle, and I encountered no problems by applying your fix. 

 

I still think that there should be a restart method. (that can be called while the tween is running or the tween was paused).

 

Thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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