02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Removing the most recent animation

Wouldn't it be great if you could regret and undo. It might be hard in real life, but not with TimelineLite. In the last few lessons, we built a really sophisticated timeline that is dynamically controlled by the user. In this lesson, we notch up the sophistication of our timeline by enabling the user to have remorse by clicking on the remove recent button and removing the most recent animation added in.

Video Transcript

00:00In this lesson, we're going to learn about two methods, not one.
00:03You're going to do two for one.
00:04So one, we're going to learn how do we fetch that latest item
00:08that we just added in.
00:11And we're also going to learn how to remove items.
00:13And we're going to do that by creating a controller that
00:16will be called removeLast.
00:18So let's get started.
00:20But it shouldn't be as complicated
00:23as what we've done before, so no worries.
00:25What I want to do is add another button that
00:27will enable us to remove the last tween that
00:30was added in to our tween library, the most recent tween.
00:35So let's go ahead and create that button.
00:37I'm going to go ahead and go right up there
00:39into our Create Controller.
00:42And I'm going to go ahead and create here.
00:44And I'm going to call this removeRecent.
00:52And I'm going to call the function also removeRecent.
01:01And let's create that function.
01:10So I'm going to read out to me all that random crazy copy
01:13that I always type when I'm creating.
01:15All right, so the first step is I want to actually retrieve
01:18the last thing that was created.
01:20And I could retrieve the last thing that was created
01:23using the timelines built in function
01:25that's called recent, really powerful.
01:28Now, it has nothing to do with where in the timeline it is.
01:31It's the most recent change that was made.
01:33So if you made a change in a different position
01:35in the timeline, it's still going
01:37to give you that specific item that was added.
01:39And we're saying that when you add an item
01:41and you place it in a specific position.
01:44All right, so I'm going to go ahead into this recent item,
01:47click on Save.
01:48I'm going to see if I'm getting, what am I getting back?
01:50So if I click your Refresh, I'm just going to add an item,
01:54move it around a little bit.
01:56And I'm going to go ahead and click on that removeRecent.
01:58And you can see that I'm getting back an object.
02:01And this object is basically that tween
02:03of moving it from some position to another position.
02:06So the next step that I want to do
02:08is I want to actually really remove it.
02:10And to remove a tween or a group of tweens
02:12or anything really, even a function,
02:15if anything that was added into the timeline to remove it,
02:19all I need to do is call the timeline itself again
02:22and call the remove function.
02:24And with the remove function,
02:26I'm just going to send into it the most recent item.
02:28Told you it won't be too complicated.
02:30So if we go ahead and click on Refresh here,
02:32let's see if it's working.
02:33So first of all, I'm going to add an item,
02:35going to move it to here, move it to here,
02:38move it to here.
02:39And let's say I'm going to move it also up here
02:40really close to our controller, our refresh button.
02:45And I'm going to click on removeRecent
02:47and then I'm going to click on replay.
02:48And let's see if it doesn't reach this final destination.
02:53Beautiful.
02:54And if I click on removeRecent again, click on replay.
02:59And if I remove another recent one and click on replay.
03:02So literally I'm getting rid of one step at a time.
03:05And now literally I only have that fade in
03:07and I could go ahead and start making new animations.
03:11So if I made a mistake, I could remove that recent click
03:13or remove it into a couple of them
03:15until I'm back to the original position
03:17that I want it to be.
03:19Now, the only problem with this removal process
03:21that I've just done here is that it's nice
03:23that I removed it, but my item stayed
03:26in the position that it was at.
03:28Wouldn't it be nice if we could also animate,
03:31just animate and reverse our item that, and why not?
03:35Because if we recall, we got that recent item,
03:39I could literally just go ahead here and save it
03:41because I know it's a tween.
03:43Well, I don't know it's a tween,
03:45but right now I know it is a tween.
03:47So first of all, let's start
03:48because we're doing it one item at a time.
03:50Let's let the first scenario work its way around.
03:56So I'm first removing it from the timeline
03:59and I could literally just go ahead here
04:01and just ask it to reverse.
04:08And if I click on save, let's see if that will work out.
04:11And reverse should basically animate it back
04:14to basically the starting point.
04:15Now, because we don't have any other extra add-ons there,
04:19so I could literally just move it one, two, three, four.
04:21And if I click on remove, so first of all,
04:24I have your reverse is not a function,
04:26re, because I did re-vise, re, ver, re, ver, verse.
04:34There we go, it even changed the color, right?
04:37That's always a good sign when things change a color.
04:39So I'm gonna add there, move it around a little bit,
04:43click on remove recent, and it's definitely going back,
04:46which is a good thing, although it's not animating back,
04:49but it's at least moving back.
04:51So that's a good thing.
04:52I'll click on replay here.
04:56So that was complete madness, complete craziness.
04:59So our problem here is that we remove the item,
05:03but the item that we're removing
05:05is somehow not exactly good enough.
05:08We probably would wanna create a separate tween for it.
05:11So what we wanna do,
05:13let's just look at the information.
05:15So I'm gonna go into the console log
05:17and just look at what am I getting back here?
05:20And I'm gonna go right back in there
05:21and click on refresh and just to analyze it.
05:24So if we're gonna go here, we're gonna add an item,
05:26move it left, move it, move it, move it.
05:28I'm gonna remove recent.
05:30And now let's look at what we're getting back.
05:33We're getting here a tween object.
05:35It has a variable with objects,
05:37which is probably the initial information.
05:40We have your total duration.
05:42Okay, so that's good.
05:44We have your time scale.
05:46Okay, and that doesn't interest us.
05:48And we have here the vars object itself.
05:52So we could click into that vars object itself
05:54to see what is actually the vars.
05:56And we could see that our X and Y coordinates
05:58are being changed.
05:59So how about we try to basically figure out,
06:05could we animate our position to go to the,
06:08basically to go in reverse?
06:11And when I'm looking at this, it seems to me that it should,
06:13but what I wanna do is instead of even reversing it,
06:16I'm gonna go ahead and go to this element
06:19that we just took out.
06:21And I'm literally gonna ask it to seek to position zero.
06:25Because I wanna see what would happen.
06:27Seek to position zero.
06:29And I'm gonna go to that and I'm gonna say
06:30I want it to play.
06:33Because I wanna see what would happen.
06:35So I'm gonna save it, click on refresh,
06:37because I'm not 100% sure what would happen,
06:38to be honest.
06:40So I'm gonna add an item, move it a little bit,
06:42move it a little bit, I'll press remove recent.
06:45So it's going to the beginning,
06:46but it really didn't play anything.
06:48So that's not a good sign.
06:51Not a good sign at all.
06:55Keep probably a good time to open up the documentation.
06:59But I'm thinking that maybe the animation itself
07:01is almost, there's probably a way to make it work,
07:06but I don't really know how to.
07:07So what I'm gonna do is I'm just gonna create
07:09a fresh new tween that is just completely independent.
07:12And what I'm gonna do is I'm basically just gonna,
07:15oh, I have a start position here, start at,
07:18and I have the end position.
07:19So if I go ahead here and just trace this out,
07:22so I'm just gonna get rid of all that.
07:24And I'm gonna do a console for a second,
07:26just to see if I can reach that information easily.
07:29So if I go to the, let's see what the data object was
07:32again, so that data object has the CSS property.
07:37It seems, for some reason,
07:39let me just do this again,
07:41so I could literally view this in the console.
07:43So I'm gonna click here, remove recent.
07:46Oh, but I, did I not save it?
07:51All right, nevermind.
07:52Let me, let me, I might've saved it.
07:54So let's see if I could, was it the CSS?
07:58Let's give it a refresh and see if that will do anything.
08:01So I'm gonna add an item, move it, move it, remove recent,
08:06and that is undefined.
08:07All right, so let me, just really quickly,
08:09my goal is gonna be to basically
08:11find out more information about that tween.
08:14So I'm gonna save that, reverse here,
08:17just literally look at the output itself
08:19and try it through the output itself
08:21to fetch the information of our element.
08:24So first of all, oh, I need to move something
08:27and then remove recent.
08:28Okay, so we're looking at our object,
08:30and I could see that our object itself has a vars object.
08:33So I'm gonna approach the vars object.
08:36Let me just make sure that I also go into your vars object.
08:41And let's look at it a little bit more deeply.
08:43In the vars object itself, I see I have the CSS
08:46and I have the start at, beautiful.
08:49So I have here that CSS.
08:51Now I know it's X and Y, so that's good.
08:58And let me just go ahead here
08:59and just copy another console
09:01because we also wanna approach the start at.
09:07And I'm gonna save that.
09:09And this is obviously non-documented, undocumented.
09:13So once you do stuff that are undocumented,
09:15don't be surprised if they break
09:17because they're not part of the API.
09:19This is really just us playing and having fun together.
09:21So that was the start at.
09:23And if I just click your refresh
09:25and make sure that I saved this
09:27and then click on refresh,
09:29let's see if we'll get that information
09:30of the start point and the end point.
09:33So if I remove recent,
09:34we sure are getting that information.
09:36All that's left for me to do
09:37is to create basically a from to tween
09:40and literally just from to those pieces of information.
09:43So I'm literally, I'm just gonna go ahead
09:45and just create really quickly a tween light
09:47from to disconnected from our timeline
09:50to what do I wanna animate?
09:52I wanna animate my T.target.
09:55How long do I want it to take?
09:57We saw that we had that in our properties
09:59because we send it through,
10:00but I'm gonna anyways set it to be a really quick value
10:03because I don't really want it.
10:04I don't really want it to take a long time.
10:08Now the start position is actually the end position for us.
10:11So I'm gonna go ahead and take this x position here,
10:14set that as our start position.
10:17Same for our y.
10:21And we also wanna create the to object
10:24and I'm gonna go ahead here
10:26and just create that object.
10:30And instead of, I'm gonna send the x and y.
10:33So we'll have here the x
10:36and we're gonna have here the y.
10:38And if I save this, very theoretically,
10:43this should really do the job.
10:45So if I'm gonna click on save, which I just did,
10:47and I'll click on refresh here,
10:49theoretically, incredibly theoretically,
10:51if I push this here, I push this here,
10:53I click on remove recent,
10:55it's gonna snap back to the original position,
10:57which is it's doing it.
10:59Yay, our logic worked.
11:02Now we do have a couple more problems
11:05because if I click your refresh and just start over
11:07and I'm just gonna add one item
11:09and add another item, that's fine.
11:11When we have your three items
11:12that we're adding separately, that's all good.
11:14That's all hunky dory.
11:17But once we switch off the auto mode
11:19and then we move more than one item at a time
11:21and then turn on our auto mode again,
11:24we're gonna replay it for a second,
11:25right, we have one, two, three
11:27and then all the three are gonna animate together,
11:29drop together.
11:30If I remove recent, it's, oh, okay,
11:33so it's only removing one item at a time.
11:36But it's not actually removing all the three items
11:38that we added at one time,
11:40which is unfortunate, but we could live with it, right?
11:42We're just, but this is exactly the type of thing
11:44that you wanna do to test out
11:46to see that everything is really working all the time.
11:48Now it won't work all the time
11:50because if we had things here
11:51that were not actually tweens,
11:54then all this logic here doesn't make any sense.
11:56But we'll solve that problem as we get to it
11:59if we add things that are not tweens
12:01and we are gonna call it a wrap to this lesson.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!