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

Our first callback: oncomplete

The oncomplete callback enables us to know when an animation completes. There are quite a few different callbacks available throughout the animation library and we will explore many of them throughout this training. In this lecture, we will understand how callbacks work and see oncomplete in action.

Video Transcript

00:00Until now we created animations and then more or less hoped for the best.
00:05We didn't have a way to interact with them once the animation started.
00:09What I want to do next is I want to talk about the onComplete and the onComplete parameters.
00:14And I want to show you the steps involved with how could you create an animation
00:18that is fluid where you could get easy access to, for example,
00:22we want another something to happen in a linear step once this logo animation is complete.
00:28So I'm going to comment out this animation tween and we're going to start a new tween.
00:32What I want to do is I'm going to create here another private function
00:37and I'm just going to create a few functions where we're going to have like four steps
00:41where step one is going to be a certain step, step two will be another
00:45and then we'll do like that three or four steps and we'll do them quickly.
00:48All right, so step one, I'm just going to get here the display object.
00:52So it's going to be a display.
00:55And by the way, this is obviously very much something that would be
01:00flash specific creating function in this way.
01:02So you would have to know your JavaScript and create your functions
01:05without explicit naming and so on and so forth and without the private and so on and so forth.
01:11But we're going to assume that you could figure that one out
01:13because it's besides that the libraries are exactly the same.
01:17All right, so I'm going to call step one
01:20and step one, I'm going to send to MC logo.
01:23And step one, we'll just basically take MC logo and how about
01:29and we're going to set its scale.
01:32Well, let's start with that.
01:34Well, not MC logo, right tween, light, dot to, from.
01:39We're going to do from, we're going to position it where we want it to be.
01:42So first of all, I want to position,
01:44figure out the target itself which is our display object.
01:47We're trying to create it and make it more dynamic
01:49so we could send different objects, different properties
01:52as we go down the line if we wanted to.
01:55Okay, so next step is we're going to set a duration.
01:57The duration will be one second or let's do a zero dot seven
02:01just to make it a little bit random.
02:03And we're going to set here the object of what do we want to set,
02:06what is the configurations that we want.
02:08So let's do for our exposition.
02:10We're going to try to make something smart
02:11because we're getting close to the end of chapter two.
02:13So I'm going to take the stage and then stage with
02:18and I'm going to divide that by three.
02:20So I want my X to be on the third, third, you know what,
02:23let's even make it a little bit in the fifth position.
02:25So we're going to take 20% of the screen.
02:27We're going to be in the 20th position, 20th of our X
02:30and we're going to do the same thing for our Y.
02:32Stage, stage, height, divide by five.
02:39Now, I'm just going to run this for a second just to see where I'm at
02:43because I feel like I'm going to have to do some,
02:45create some changes in this animation.
02:48So first of all, what's going to happen is we're starting really
02:51from where we wanted to, but we're going back to that initial position
02:54which is not really where I want to go.
02:56So what I want to do is I want to kind of,
02:58instead of doing this from and to,
03:00and I'm trying to avoid using the,
03:04I'm trying to avoid using methods that do exist in Tweenlight
03:07that do not exist in Tween.
03:09Oh, for now, we're going to slowly add more methods
03:11that will make our lives easier.
03:13So I'm going to use the to command,
03:14but instead of setting a long amount of seconds,
03:17I'm going to set it to be zero.
03:18Notice what will happen if we do that.
03:20It's going to take us immediately to that position
03:22without actually taking any time.
03:24So by setting it to zero,
03:25we're more or less saying move to that specific position.
03:28And we're going to see later on,
03:30how could we do that in a more simple way,
03:32in a more intuitive way?
03:33And I'm going to go to scale,
03:37scale X.
03:40We're going to set that to be at 0.5,
03:43which is 0.5.
03:44Scale Y, we're going to do 0.5 as well.
03:47So let me just see where we're at.
03:48And we can see that our,
03:49now our logo is not too big.
03:51It's in a normal size
03:52and it's sitting there in the corner of the screen.
03:54And literally what I want to happen
03:55is I want our animation to go from here to here
03:58to here to here to here
03:59and just basically circulate in R square,
04:03maybe even making it endlessly roaming around in that circle.
04:07So let's do that.
04:08So let me move,
04:09so first step, step one was positioning it
04:11where I wanted it to be to position.
04:13Perfect.
04:14Next step is I want to actually create my animation.
04:16So I'm going to go to another two,
04:18the same display.
04:19This time I want to take it,
04:21let's start with 0.7.
04:22You know what?
04:23Let's make this,
04:24if we're already doing things a little bit smarter,
04:26let's create here a private var
04:27that will be the,
04:30will be time, anim time,
04:36which is going to be a number.
04:38We're going to set that to .7
04:39and we're going to play around with that value
04:41and see which one,
04:42which, you know, let's set it to const
04:45because we're not planning on changing that value dynamically.
04:49Anim time.
04:52And I'm putting everything in capital lock
04:54because a constant, by the way,
04:56is basically a value that you could never change.
04:59As soon as you start,
05:00this is a constant.
05:02This is the value that will be
05:03throughout the duration of the application.
05:05It's not a variable, it's a constant.
05:07So I'm going to set that value in our area here.
05:12So the amount of time we want it to take is 0.7 seconds,
05:15but that way we could easily change it
05:17before we publish and play around with the value
05:20that it should update throughout our application
05:22if we use that anim time as our core,
05:24as our definer.
05:26And next is we want,
05:28next is we want to set our position.
05:30So I'm going to set my x position
05:32and I'm just going to copy this again
05:35and I'm just going to copy my x and y positions.
05:38But this time around what I want to do is
05:40if we were at the first fifth,
05:42if we were at the fifth of the stage,
05:44now we want it to be on the 80th percent.
05:46So I'm going to times 40 times for that
05:49because this is the 20th percent area.
05:51So times four will put me in the 40 percent area.
05:54For the stage itself,
05:55I don't actually want to change the y
05:57because I want it to animate from x
05:59and kind of like I want my x to move.
06:02I don't want my y to move at all
06:04so I'm not going to animate my y at all
06:06so we could get rid of that.
06:08So let's take a look and see if our animation
06:10is actually moving and it sure is.
06:12So you see how it's going from position one
06:16to position two.
06:17Next we want it to move from position two to three
06:19but we need to know when this animation completes.
06:22Now although we could create this animation
06:24by just creating right ahead another animation
06:27and just adding to this new animation
06:29if I set it and let me just set the right values here,
06:31so the x is really that x
06:33but we want to now set the y property
06:35and the y would be that 20 percent times four
06:38because we want it to get to that 80 percent.
06:40So that's all good so far.
06:42And all that's left for me to do technically
06:44is to add another delay here
06:46and to physically add a delay of one second
06:49or of the actual amount of time of the animation
06:52of the first one which was of the anim time.
06:55And if I do that I really would get that animation
06:57where it would go from step one to step two
07:01but it's forcing me to, it makes it probably
07:03a lot less easy for me to then monitor and tweak
07:07if I'm not very comfortable with it.
07:09And two, what if I wanted to do a few other stuff
07:11in the process as it's animating,
07:13maybe as it's going down I want to do another something
07:15in the process.
07:16It makes it impossible for me
07:18or it makes it much harder for me
07:20and it would make my code more bloated.
07:22What I really want to do is I want to move this
07:24into some sort of a function that would be step two
07:27and maybe private function, step two
07:31and that's really what I want is that I'm going to,
07:35step two doesn't have the object
07:37because it's already inside of it
07:39and we're going to try to figure out how to,
07:41you know, step two, we're going to have the display
07:45move with it as well which is going to be exactly,
07:47now before we're not going to have a delay at all
07:50because this function should only be called
07:52when this function completes.
07:54Now the way that we get it to work is very simple.
07:56We add another property which is called OnComplete
07:59and on complete we want to call step two.
08:02Now by default it's not going to send anything
08:05and it's going to lose its scope.
08:07It's not going to know what object it's animating
08:09or would it.
08:11So it might know it, so let me if I comment this out
08:14and I comment this out and I put here just a little trace here
08:17and I trace out this, let's see what this is.
08:21So we can see that this is actually my stage.
08:25So I don't have any way to refer back to my actual tween
08:30or to refer back to the object that's being animated
08:33and that's the reason why I want to pass Oto the actual object.
08:37So as I'm passing the actual object
08:39I need to pass it on as an extra parameter
08:42and to do that it's called the OnComplete params.
08:45So OnComplete params and it's expecting an array
08:51and we're going to send it into array as the first item
08:53the display and the reason why it's, well the display
08:56is still visible to step one but it wouldn't be to step two
08:59because we're already outside of that function.
09:03So this is Oto a display object, all right.
09:10And if we do here a little trace before we do anything else
09:13we should see that our display has been passed through
09:16and we should be able to get our display in our output.
09:20And that means that we could continue on with our animation
09:23continuing on to our next step
09:26and literally this is exactly the process that I'd want to do
09:30to Oto complete the next steps.
09:32So I'm going to go ahead and start working on the next step.
09:36So, oopsie, what am I doing here?
09:39So I want to copy the OnComplete and OnComplete params.
09:43I don't really want to change anything in the OnComplete params
09:46but I want to call step three
09:49and I'm just going to call step three.
09:51I'm just going to just duplicate my step two function
09:54and I'm going to call this step three.
09:57And in step three do I want to animate the Y?
10:00No, I only want to animate the X.
10:02So I'm going to get rid of that Y
10:04because I'm not animating it really.
10:06And I want to bring it back to that original X position
10:08that it was in.
10:10And once I'm done with that I want to call step four.
10:14So literally you can see how that eased my process
10:16and at any stage here I could have added extra information
10:19or extra things that are relevant
10:21and we'll just do that in a second.
10:23So once we get to step four we don't need to call the OnComplete again
10:26unless we want to loop this.
10:28And if we wanted to loop it then we would call step one again.
10:31So before we even do that let me just comment that out.
10:35Let's do one step at a time.
10:38And the next step is we want to take our OnComplete
10:41and we want to kind of go a full circle
10:43back to that initial values that we set to our Y
10:46when we started our animation.
10:49And there we go.
10:52So we don't need this apostrophe either.
10:55Now let's give it a run.
10:56So in theory we should have your full loop
10:58starting from the beginning
10:59and going all the way to the beginning as one loop.
11:03Now again as we said if we wanted to
11:05we could have added that OnComplete again
11:07but this time around sending us back
11:09to the first one creating an endless loop
11:12that goes between these five animations
11:14that we created or four animations.
11:16The first step, the second step, the third step, the fourth step
11:19and then it would take us back all the way
11:21to the first animation.
11:23Let me see I just had your little error.
11:25I must have lost a comma here somewhere.
11:31Oh yeah because I closed our object a little bit too early.
11:35That's it.
11:37And there we go.
11:38And now we will have an endless loop
11:40where our animation is going to go
11:42in basically forever
11:45and it will never stop unless we do something to stop it.
11:48So that's it.
11:49That was OnComplete and OnComplete params.
11:51What I'm going to do in the next session
11:53is I want to talk about the OnUpdate
11:56and we're going to see what can we do with the OnUpdate
11:59in the following 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!