tips4design Posted August 21, 2015 Share Posted August 21, 2015 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 More sharing options...
tips4design Posted August 22, 2015 Author Share Posted August 22, 2015 Bump Link to comment Share on other sites More sharing options...
tips4design Posted August 23, 2015 Author Share Posted August 23, 2015 More details here: http://stackoverflow.com/questions/32165940/phaser-re-use-tweenjsFiddle here: http://jsfiddle.net/5qzjjegt/7/ Link to comment Share on other sites More sharing options...
tips4design Posted August 23, 2015 Author Share Posted August 23, 2015 **LE:** Here is a fiddle: http://jsfiddle.net/5qzjjegt/9/ I reduced the problem to this: if a tween is stopped it can not be started again? This is working (note how the tween ends naturally as the loop time is longer): http://jsfiddle.net/5qzjjegt/10/ Link to comment Share on other sites More sharing options...
Tom Atom Posted August 24, 2015 Share Posted August 24, 2015 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 More sharing options...
tips4design Posted August 24, 2015 Author Share Posted August 24, 2015 The new target is an instance identically with the first target. It will save the computing done to caclulate the tween steps (timelines).See this issue, maybe you have an idea how to solve ithttps://github.com/photonstorm/phaser/issues/2021 Link to comment Share on other sites More sharing options...
stupot Posted August 24, 2015 Share Posted August 24, 2015 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 More sharing options...
tips4design Posted August 24, 2015 Author Share Posted August 24, 2015 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 More sharing options...
stupot Posted August 24, 2015 Share Posted August 24, 2015 Ah yes, didn't read your 1st post fully. Link to comment Share on other sites More sharing options...
Tom Atom Posted August 24, 2015 Share Posted August 24, 2015 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 tips4design 1 Link to comment Share on other sites More sharing options...
tips4design Posted August 24, 2015 Author Share Posted August 24, 2015 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 More sharing options...
Recommended Posts