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

Killing animations

Not only can animations be started using the GSAP methods but they can be terminated as well. To terminate an animation we will use the kill method. In this lecture, we will learn how to use the kill method to terminate animations.

Video Transcript

00:00I truly hope you're not excited about the fact that I told you we're going to be killing things
00:04in this lesson. So if you are excited, I think you should go see a doctor. But if you're just
00:11kind of like, okay, let's see how we're doing it, then you're in great shape, let's see how we kill
00:15things. So what we're going to do is we're going to add here, click events for our MC logo. So
00:21I'm going to go to MC logo dot add event listener. And I'm going to call that mouse event.
00:27Oh, what am I doing? Yeah, yeah, yeah. Okay. Now, event click on click. And we're going to create
00:37that function private function on click. E mouse event. All right, there we go. So once our movie
00:47clip was clicked, what I want to do is I want to go to my tween light to not to always go to to
00:53kill, kill, kill tweens of MC logo. All right, there we go. So we want to kill all the tweens
01:03of MC logo. All that's left for me to do is run the application and actually try to capture it
01:08because it's going to be moving quite quickly. So I'm going to just wait for it, wait for it,
01:13wait for it. There we go. And I stopped the animation. By the way, you could call it a
01:18few times. It doesn't really matter. Because once it's killed, it's killed, it's not going to
01:21create any blower of any sorts. So that's how you kill a specific movie clip. But what if you wanted
01:27to kill a specific tween? And that moves us into a new world that we haven't noticed so far. So far,
01:34we were working only with the static functions to and from. And in the future, we'll see the
01:40static method of from to or to from. And what we really want to do next is we want to be able
01:46to capture the animation. Mainly, we want to capture the animation of T of tween light of the
01:53of our text field, because we want to kill it auto. But if we tried to kill our text field,
02:00nothing would happen. Let me show you why. And you know, I'm going to show you and then
02:03think about why wouldn't it actually do anything if I would kill the tweens of our text field.
02:10Okay, you're thinking about it, thinking about it, I'm giving you another second to think about
02:13it. And notice when I finally get it, and I stopped it, the text continues to complete itself.
02:21Now, the reason is, if we look back at it, and if I hope you already know the answer,
02:24but if you don't, the reason is because we're not really animating the text, what are we animating,
02:28we're animating this object, Oh, helper. Now, Oh, helper is not the text field. So we're
02:33stopping all the animations of Oh, helper. But are we really stopping the animations of Oh,
02:39helper? No, because we're stopping the animation of the text field, which has nothing to do with
02:42Oh, helper. And Oh, helper is continuing to animate, and thus the animation continues.
02:47So what we really want to do is we want to capture this tween. Now, the way we do that,
02:51I'm just going to create a new variable here, a private variable, and I'm going to call this
02:57text tween. And it's going to be a tween light object. And the reason is, is because our tween
03:05light actually returns to us the value when we create the value. So basically,
03:10if I just set the value of a object that is of a type of tween light to equal the value of the
03:17animation that we're creating, it's going to return us our text tween, our tween, our tween
03:21light, and we're going to assign it into the text tween. All that's left for me to do is then to
03:26set that kill tweens of instead of kill tweens of because we're not the tween, the text tween
03:33is actually a tween. So to do all I have to do is approach the text, text tween and call the
03:39kill function. And that's it once I call the kill function is going to kill all the tweens
03:45in of this object. So if I run it again, and I managed to capture it before the animation is
03:52complete. You'll see that both the text and the tween of our mouse have terminated. So there you
03:59go. That's everything we wanted to talk about killing. And in the next video and the next
04:04lecture, we're going to follow up and wrap up this chapter talking about a few few
04:10small things that you might want to know about.

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!