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

Toggling the play state in TweenLite

Toggling the play state in TweenLite is just one of the many features available in TweenLite,making it possible for you to create complex animations that are immensely controllable. In this lesson, we will learn how to stop, pause, and play animations – and even create a toggle control button.

Video Transcript

00:00All right, so let's continue. This is lovely, this heartbeat. But what I want to do is I want to learn
00:04a lot more properties and things we can do with animation. So let's, for example, add a pause and
00:09play again. So let's see how we do that. And by the way, we let's just start doing and see how
00:14we do that. So first of all, what I'm going to do is I'm just going to add a new click event to
00:18our MC logo. And I'm going to add event listener. And I'm going to add it to the mouse event that
00:26click. And I'm going to call that on click and create that function, arrive at function on click
00:38mouse event. Beautiful. So whatever the click happens, very easy, we want to pause it. Our
00:47problem is, is we don't have a reference to our tween. And beyond that, maybe there's a couple of
00:53tweens going on at the same time, maybe we're tweening on our x scale, y scale, we're doing
00:59one thing. But at the same time, we might be doing another tween. So let's say, let's say
01:04bit really, and I'm going to add here another. Let's not do it. Let's not do it. Imagine that
01:08there's a couple of tweens going on at the same time. Maybe we even created some sort of
01:13elasticity, a deeper elasticity. You know, I do want to show you how to I do want to show it.
01:18Okay, so I'm going to take this scale, y, just snap it out of there. And I'm just going to
01:24create another tween right with it. And I'm going to, instead of scaling the x, I'm going to scale
01:30the y. So one tween is scaling the x one scale tween is scaling the y. And all I'm going to
01:36do is I'm going to change the amount of time to zero to seven, because it's going to create this
01:41weird bountiness that's going to distort our logo, because we're going to have different
01:46timelines. And notice, because we didn't have any direct reference to our tween itself,
01:51but we were using the self, we're actually dynamically sending in different names so we
01:56can use the same method as we were using before. So let's run it again. And notice how our logo
02:02is now going to be squeezing up and down and getting distorted all the way up as it's turning
02:07around, making it very virtually unique. Now, if I wanted to stop both tweens at the same
02:14time, I wouldn't be able to reference to one or the other. What I really want to do is I want
02:18to get all the tweens of the specific animations that are going on right now. To do that, I could
02:25call the tween light the tweet, not tweet light, that sounds good though, tweet, tweet light,
02:33tween light, and I'm going to get the tweens of a specific target. What is our target? MC logo.
02:39And if I'm just going to trace this out for a second, you're going to see that we're going
02:43to get an array back and in that array, once we click on it, we're going to get an array back in
02:48that array. We're going to get all the tweens that are right now working on that specific object.
02:53All that's left for us to do is to basically save this into an array. So I'm going to get a tweens
03:00and I'm just going to get all this information right into our object. And I could loop through
03:06it for var for each var item object in a tweens. And all that's left for me to do is to call my
03:19command. So I'm going to go to my item, which is basically a tween. So let me just change that to
03:23tween. And I'm going to go to tween. And I'm going to call one of the functions of tween,
03:30which is pause. And if I run my application again, and click on it, we'll see that we're
03:38going to pause both tweens at the same time. And the reason is because I'm looping through all the
03:43objects, all the tweens are inside our a tweens array that we got from get tweens of MC logo,
03:49and then I've paused them all. Now my only problem that I have right now is because I'm looping
03:54through them, and I'm always pausing them. My problem is, is that I'm never going to get
03:59a way to replay them because if I click again, it's going to pause again, where what I really
04:04want to happen is to have some sort of a toggle. To do that, I'm going to take advantage of the
04:09paused method instead of the pause. So I'm going to comment that out. And I'm going to call the
04:17tween paused. Now if I call the pause, and I don't give it any reference, all it's going to
04:23do is it's going to trace the current status of paused. So it's just going to tell us is it's
04:28paused, or is it playing? And in our case, it's paused. What we really want to happen is we want
04:33to actually tell it to be the exact opposite than what it is. We want to toggle it. So to do that,
04:38all we really want to do is we could use this little neat trick that's available in ActionScript
04:44and also JavaScript is I'm going to take the value of paused, and I'm going to flip its value by
04:51putting here a command line, and then tween paused, which basically right now it's false,
04:59it will flip it into true and then send it into that function again, but this time because we're
05:04setting a value, it's going to automatically set it to be true or false. So first time around,
05:10it's going to set it to true. Second time around is going to set it to false, and we're doing it
05:14throughout the different tweens. And by the way, we could have done it to a lot more tweens,
05:17not only of our specific animations. We could have, for example, paused all the tweens that
05:22are happening on the stage and replayed them at any point that we wanted to.
05:26Now, I wanted to also talk about another thing. So when we talked about the pause,
05:32we talked about the the pause function, which we are basically using as a toggle. Now, if we wanted
05:40to, and I'm not going to do it because we already have played around with it, I just want to talk
05:44about the difference between play and resume. Play always goes forwards, resume goes in the
05:55direction that we're going. So if we're in the middle of going backwards, it will resume going
05:59backwards. Contrary to that, if we clicked on play, if we set it to play, it would always go forwards.
06:04So play always goes forwards, resume goes in the direction that we were at, we're just
06:09resuming the direction that we were at. In our case, setting it to pause, or on pause,
06:14we'll just reserve will basically resume it back to what it was before, or pause it. Okay,
06:20so that so we covered quite a lot of different functions. Right now, we've learned about the
06:23tweens of, we've learned about the pause, the play, the resume, and the paused, and how to work with it.

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!