Nesting timelines
Nesting timelines into other timelines. This changes time completely and is one of the most exciting things about the GSAP library. Armed with the skills we developed in the last few weeks, you're more than ready to jump into this really fascinating aspect of the library. In this lesson, we will nest a TimelineLite inside of our main TimelineMax timeline.
Video Transcript
00:00Alright, we're ready for the last major topic in this course, and in this chapter.
00:05And what we're going to do in this chapter, in this lesson, in this chapter, in this course,
00:09in this ending, one really big thing.
00:12We're going to actually add a timeline into a timeline, creating nested timelines.
00:17Very cool stuff.
00:18Not that complicated at this stage because we've been through so much together.
00:22But in the next video, also, we're going to talk about some really important things
00:25that weren't too important for us until now.
00:28So don't forget to watch the next lesson as well before we wrap up this course.
00:33Last major topic, which I'm incredibly, incredibly happy to be here at this stage.
00:39And I hope that you are incredibly proud of yourself because you've been through a lot
00:44if you've been following all of our courses to get to this stage.
00:48So the main topic of this lesson, and really, it's a huge, cool topic.
00:53Not very complicated at this stage after everything we've done, is that timelines could have timelines
00:58nested inside of them.
01:00That is one of the most cool things about this animation library and one of the most powerful
01:05things.
01:06Although, the sample that we're going to do right now isn't a very perfected sample
01:11because this is something that will create a lot of bugs in our editor because we didn't
01:15plan to have so many animations going on at the same time.
01:19But with it, you're going to learn a really crazy skill and hopefully you're going to
01:22take this project that we've built together, together, together, and make it better.
01:28That is your goal and I'll be with there throughout it.
01:31So send me emails, send me comments in the comments section.
01:34I want you to feel like this project is your project and I want you to mature it, turn
01:38it into a real complete editor because we're not very far from that.
01:43Without further ado, let's start doing it and let's start adding a timeline inside
01:47of a timeline.
01:49So to do that, I'm going to go ahead and first of all, I'm going to need another
01:52secondary timeline.
01:54So I'm going to go ahead here and call this timeline inner and not going to put
01:58anything inside here in the meantime, but this is going to be the placeholder where
02:02I'm going to add my animations into them before I add them into my main timeline.
02:07Now the next step is I want to actually add an add timer, so I'm going to go
02:11ahead here and create another controller.
02:14And I'm going to call this add timeline and I'm going to call the add
02:20timeline method and let's go ahead and create that method.
02:27So I'm going to scroll all the way to the bottom, find a nice empty spot and let's
02:34create that function.
02:37All right, so step one now that we created the function is we need to figure
02:41out, are we in the timeline right now?
02:44Are we not?
02:45And the easiest way to do that is to literally ask if TL inner, which would
02:49basically mean if our inner timeline exists or not.
02:53If it doesn't exist, what do we want to do?
02:56If it does exist, what do we want to do?
02:57We want to take that timeline and add our inner timeline into our timeline.
03:06Literally nesting the new animation that we're just about to create in a minute
03:10right into our timeline.
03:12Next thing that I want to do is I want to make sure that my timeline inner will
03:15now equal nothing.
03:17So I'm going to set it to null.
03:19The next step that I want to do is I want to change the copy of my button
03:23to say, well, Mr.
03:26Button.
03:27Now you will be again, text add timeline.
03:33All right.
03:33This is assuming that we already have a timeline.
03:35We have already the animation configured.
03:37What we want to do now is assume the other part.
03:40So there is no animation.
03:41One wasn't created yet.
03:43In that case, what do we want to do?
03:44We want to create that inner timeline by creating a new timeline max.
03:49Now I want this animation to repeat itself twice.
03:53So I'm going to go ahead and use the timeline max properties of repeat.
03:59And I'm going to set that to repeat two times by one would be 0 and 1.
04:04And I want it to yo-yo, just like we would have with the tween max.
04:11All right.
04:11So I set my yo-yo and repeat.
04:12So it's going to go into the direction that I animated it
04:16and back to the beginning.
04:17Beautiful.
04:18So that is our rule.
04:19Next step, what do I want to do?
04:21I want to go ahead and continue and just change also our text here.
04:25So our button will reflect the fact that we need to close the timeline.
04:31So the idea behind here is recreate a timeline.
04:34And when the user clicks again, we close the timeline.
04:36So that's all the logic we need really to add new timelines dynamically.
04:40Now imagine what you could do if it wasn't dynamically.
04:42This is really taking it to the edge here with our training.
04:46Now the next thing that I need to do is to make sure instead of calling the TL
04:50and add into the TL every time there is an addition,
04:53every time there's something that's going on, instead of that,
04:56I really want to make sure that I'm referring to our inner timeline whenever
05:00the inner timeline is open and referring to the main timeline
05:04when the main timeline is open.
05:05So to do that, what I'm going to do is I want to create here
05:08a function that would just return to me the timeline that we're in.
05:12And I'm going to go ahead and just return or the inner timeline if it exists.
05:17And if it doesn't exist, I'll want to return the main timeline.
05:21Now if you know object-oriented programming, then obviously a lot of the stuff
05:24we're doing here is not 100% efficient because we're not doing it completely
05:28in object-oriented mode.
05:30And I encourage you to do it in object-oriented mode.
05:32I just didn't want to throw that in to mix things up and make
05:36our course even more complicated.
05:38So our real goal is to just focus on the animation and focus on the logic
05:41and focus on the flow.
05:44All right, so now that we got the get timeline,
05:46the next thing that I want to do is I'm going to open up my find
05:48and replace in my editor here.
05:50And I'm just going to find that.
05:52And in my find and replace, I want to search for every time I say timeline
05:56and replace it with the get timeline.
05:58So I'm going to go ahead here and just find and find
06:00where do I want to change it.
06:01And the get's yell, I definitely don't want to.
06:03Nothing in the timeline function either.
06:05So I'm going to skip ahead and move all the way to the beginning.
06:08Definitely not in the initial variable.
06:11But OK, now that we're changing labels, I'm going to go ahead and add that.
06:14Although we're not going to add labels into our animations
06:16because remember that we have pauses.
06:18And if we add a pause inside, then our logic right now
06:22wouldn't actually fully work.
06:23But I'm going to go ahead and replace anyways
06:25any place where the timeline is.
06:28Call directly and call it through our timeline function.
06:31Again, I'm not making it the most effective right now
06:35because I'm really focused right now on getting through
06:37to show how to add timelines in.
06:39So literally, I'm just doing it.
06:41There are better and more efficient ways of doing it.
06:44And I encourage you to make it more efficient.
06:46So for example, instead of calling the get's yell three times here,
06:50we could create a variable once and then reuse it
06:53throughout that method.
06:54But we're not doing that.
06:56Another efficient way would be to have basically a current timeline
07:02and then refer to the current timeline as an object,
07:05but then store the actual what timeline it is separately
07:08so it's not in a method each time.
07:11And that would probably be a much more efficient mechanism
07:14and a lot smarter because we're not
07:15calling a function 100 times.
07:17We're only calling we don't even need that function.
07:19So even if let's do it this way, and if we have any time,
07:24we're going to do it the smarter way also.
07:26But I'm going to continue.
07:27So I'm going to replace any spot
07:29that I have there that original timeline
07:31and just call that method get's yell.
07:36All right, everything here is good.
07:39And again, this is not the efficient way.
07:41It's fine.
07:41I don't mind that it's not the efficient way.
07:43You should mind, especially if it's in a live project,
07:46you want to be the most efficient that you can.
07:48And right now, we're adding 100 functions that
07:51are just not needed.
07:52And there we go.
07:52That one was on that one we want to undo.
07:56OK, so we could get rid of that find and replace button.
08:02I don't even know how to get rid of it.
08:04There you go, escape did it.
08:06All right, so we saved and we changed everything.
08:08And now everything should work.
08:09So if we go ahead and give it a run,
08:11if we don't have any errors, what should happen,
08:14and I'm just going to go ahead.
08:15We have an add timeline, but I'm going to go ahead
08:17and just add one item here.
08:20Going to add another item here.
08:22Going to add another item here.
08:24And another item here.
08:26So that'll be step one.
08:27And I'm going to go ahead and turn on my auto mode
08:30and then kind of like just position my items in a just
08:34a zigzag kind of layout.
08:37I'm going to move to non-manual again.
08:40I'm going to add another item.
08:41And now it's time for me to test out my add timeline.
08:44So what I'm going to do next is I'm going to click
08:46on add timeline.
08:47Now I have a new inner timeline and I'm going to literally
08:50just go ahead and just animate this item.
08:52And we could see that it's literally going through
08:55this animation each round.
08:58And you can see that it's going animating back
09:00to that initial position because it has a yo-yo going on here.
09:04So if I go ahead and I'll see that it's going to always
09:06return back to that initial position, which is fine.
09:09That is part of the game right now.
09:13And I've added that timeline.
09:15I could close that timeline.
09:17And I'm going to go ahead and grab this item again,
09:19push them to the other side.
09:21And I'm going to add another timeline.
09:22And this time again, I'm going to go ahead to this point.
09:26I'm going to go ahead to the second point.
09:29I'm going to go to the last point.
09:32And last but not least, the really last point.
09:36So now that I've created two timelines,
09:38all that's left for me is to close that timeline.
09:40And I could literally press on replay
09:42because I don't have any pauses.
09:44And I don't have any labels going on anywhere.
09:46I didn't add any manual pauses.
09:48I could literally just click on replay.
09:50And it should start from the beginning,
09:52add all those elements, position them into that zigzag,
09:55bring that new item, which is in its own timeline.
09:58So it's going to go back and forth
10:00because it's in a yo-yo logic right there.
10:04And then it's going to do the next step.
10:08So there you go.
10:09And that is a perfect example of how
10:11you could nest timelines inside of timelines.
10:13And the news is you could nest the timeline
10:15in a timeline in a timeline.
10:17And you could create really sophisticated animations
10:19that way.
10:20And this really covers our main topics of this course.
10:23There's only one thing I want to talk about that
10:25is a little bit theoretical that I want you to be aware of.
10:28And we're going to talk about that in the next lesson, which
10:30relates to time, which is something I promised in a very
10:33early stage of our training.
10:35I want to go through all the time controllers
10:38and explain the differences between them
10:40now that we've seen all the timelines
10:43and all the features of our animations.
