Jump to content
Search Community

Why can't you nest one Timeline instance in multiple Timelines?

KVMaster test
Moderator Tag

Recommended Posts

From the TimelineMax FAQ I understand that Timelines can only appear in one parent Timeline at a time. I'm just curious what the reason for this is. In my current project It has the unfortunate consequence that any Timeline that uses a "shared" child Timeline will have to be cleared and remade every time it's played. Which is not a huge deal, I'm just curious more than anything.



Link to comment
Share on other sites

Great question. There are several logistical issues that would really complicate things (or make them downright impossible) if a single tween or timeline could exist in multiple places (timelines) at the same time. For example, what happens if parent 1 tells it to render at a completely different time than parent 2, but both are running simultaneously. Basically, you'd end up with things jumping around and it'd waste CPU cycles. Plus, when you try to figure out the time() or totalTime() or progress() or totalProgress() of that child tween/timeline, which would it report? It'd be getting multiple instructions for different places, so I suppose the last one would win, but that could lead to a lot of confusion. 


Also, let's say a tween exists in timeline1 and timeline2, and you pause() timeline1 - would that tween also be paused in timeline2 while all the rest of the children play? If so, that would violate the consistency in the way play/pause works in the rest of the platform. If not, what happens when you unpause() timeline1? What state would that child tween render at - the state it was in when timeline1 was originally paused or the current state? 


If you kill() timeline1, how would that affect the child that also exists in timeline2? 


If you want to get the child's startTime(), how would it know which one to report if it's in multiple timelines? It could be different for each parent timeline. 


Also, it would introduce some performance issues because whenever you perform an operation that affects a child's startTime (like reversing, changing the timeScale, seeking to a new time/label, etc.), it would have to loop through all of its parents and edit each of them, and store the startTime uniquely for each timeline. It would eat up memory and prevent us from using linked lists under the hood in the most efficient way. It would also negatively affect the algorithm used to manage overwriting because it'd have to calculate the overlaps in multiple places for a single tween.


There are a few other reasons but hopefully this adequately explains why each tween/timeline should only have one parent. 


All that being said, you CAN get a similar effect with some fancy footwork. Here's how:


Create your tween/timeline (I'll call it child) that you want to exist in multiple places, and pause() it. Then, you can actually create tweens of that child's "time" and place those tweens wherever you want. And remember, TimelineMax has tweenTo() and tweenFromTo() methods that make this easy, and they spit back a tween instance.  For example:

var child:TimelineMax = new TimelineMax({paused:true});
child.to(...); //populate it with whatever tweens you want.
//now, let's create some parent timelines:
var parent1:TimelineMax = new TimelineMax();
var parent2:TimelineMax = new TimelineMax();

//drop tweens of the child timeline into the other parents
parent1.add( child.tweenFromTo(0, child.duration()), 5); //just like adding the whole child - it tweens from time:0 to the end, and we're dropping it into the parent1 at a position of 5 seconds.
parent2.add( child.tweenFromTo("label1", "label2"), 10); //or use labels. 

This works because you're using individual tween instances and dropping them into place, but those tweens are essentially scrubbing the virtual playhead of the child timeline. 


Make sense? 

  • Like 2
Link to comment
Share on other sites

That makes total sense, thank you so much for explaining that. The code you posted is hugely helpful as well, I had not even considered tweening the timeline... I think that will solve my current issue quite nicely.



  • Like 1
Link to comment
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.
  • Create New...