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

Creating a callback and activating the Que

In the last lecture we started to store animations that where not ready yet to trigger. In this lecture we continue down that path by going into our current animation logic and asking it to update us when it's done using a callback. Once we have that logic in place we have everything we need to create an animation engine.

Keynotes:

  • Adding callbacks.
  • Managing animations.
  • Getting it all to work.

Video Transcript

00:00In the last lecture, we created a queue,
00:02but that queue is useless to us as long as animation
00:06that is just happening does not update us that it's done.
00:09For that, we're gonna use a callback and activate our queue
00:13to get our animations to run one after each other.
00:16With it, we'll learn about how to add a callback,
00:18what they are.
00:19We're also gonna learn how to manage our animations,
00:22which will complete our process of creating
00:25and getting everything to work together
00:27so we could queue in and send in multiple animations
00:30that will happen one after each other.
00:32So let's jump right into it and let's finish up
00:34creating our mini animation manager.
00:38In the last lecture, we started creating our text queue
00:41and it actually is in place, but it's not really doing much
00:44because the next step for us is we really need to know
00:47when an animation completes.
00:49To do that, we're gonna have to go back
00:51and revisit the logic in our text in title
00:54and add to it some sort of mechanism
00:56to let us know when we're done.
00:58So to do that, I'm gonna go ahead and add a function
01:01that's called callback.
01:03Now callback functions are functions that are basically,
01:05we send a function in telling that whatever is being used,
01:10hey, by the way, let me know when you're done.
01:12Now all that's left for us to do
01:13is to figure out when are we done.
01:15So first of all, we have also when we call our timeout,
01:18we have to make sure that we're gonna send in
01:20that new parameter, which is called callback.
01:23Now that we've done that, the last item that's left
01:26is to find out when are we completely done.
01:27This is really where it happens.
01:29If the length exists, then we're not done,
01:31but if the length is not done,
01:32then let's go ahead and call our callback.
01:35And I just noticed that I didn't put an open
01:37and closing brackets in our if conditions,
01:40although not mandatory, a lot of times
01:41people get upset at me that I don't
01:43because I personally don't like to
01:45when it's only one line.
01:46So let me just go ahead and put that there
01:48so whoever hates it when I do that will love me again.
01:53Beautiful.
01:54Okay, so now that we set that in place
01:55and I set my callback to be called,
01:57I'm also gonna go back into this function
01:59when it's called and make sure that I'm calling a callback.
02:02But I'm gonna also create that callback.
02:04So let's go ahead and do that.
02:05I'm gonna also put that inside of the world
02:08of our init function.
02:09So right after our qtext,
02:11I'm gonna create another function.
02:13This time around, I'm gonna call this function
02:15and you name really would work,
02:17but I'm gonna call startNext
02:19because that's literally what it does.
02:20You could also give it some sort of a callback name,
02:22which would be on and then the event that happened,
02:25which is a more common name.
02:27So you could call this, for example, on text complete,
02:32or some sort of name similar to that.
02:34All right, so on text complete,
02:35our callback will be called,
02:37which means that animation is done
02:39and it's time for us to create another animation.
02:42To do that, I'm gonna go ahead and create a few variables.
02:44One is gonna be for our text like we've used previously
02:47and the next one is gonna be a variable that's called,
02:50let's just call it object,
02:51which is gonna be whatever's inside of our TQ.
02:55Well, actually in this case,
02:56all I'm gonna have to do is just go to this TQ
02:58and approach our last item there
03:01or the current item that's sitting inside there.
03:03So I'm gonna go ahead to my TQ
03:05and just shift out an item out of it.
03:07And once I've shifted that item out,
03:09taking it from the first position,
03:11I could start animating and doing the exact same thing
03:14as I've done before.
03:15So let me just also make sure
03:16that I'm gonna send this parameter,
03:18this function as that callback.
03:21I also wanna make sure that before I actually call this
03:23and recall this item once we're inside of here,
03:27I wanna check to see if text exists,
03:30something's inside there.
03:31And only if text exists,
03:32I'm gonna go ahead and actually call
03:34that same structure again.
03:36What I've done here is I've transitioned the steps
03:38from just walking through this one more time.
03:42When I'm creating my animation,
03:43I'm sending in a function,
03:45which will be called when the animation completes.
03:48In text and title, when it's all done
03:50and it finished doing everything it needs to do,
03:52it's gonna call that callback,
03:53that function that was sent in.
03:55Once that function is called in,
03:57it's gonna go back to that TQ,
03:58our queue of texts.
04:01We're gonna get a text from there.
04:03If there is something in there,
04:05if we get something from there,
04:06then we're gonna go ahead and continue down the path.
04:08We could have also structured it
04:10in a little bit differently by just saying
04:12if their TQ has a length, TQ length,
04:17and just wrap up that length,
04:18it really doesn't matter how you structure it.
04:20In this case, I just went ahead
04:21and grabbed that item immediately
04:23because if I grab something and nothing's there,
04:24it's just gonna send nothing,
04:26which works for me as well.
04:27All right, let's go ahead and save this
04:29and see if this works.
04:30And if I go ahead here and click here on refresh,
04:32which is the first item animating,
04:33just like it did before,
04:34and then the second item animating,
04:37and we could have put it in
04:38and continued as many items as we wanted.
04:40Now, we do have a slight problem here
04:42is that our bottom item,
04:43we really wanna clear it out before we move on.
04:47To be able to do that,
04:48what we wanna do is we wanna revisit this queuing process,
04:51and instead of putting into the queue
04:53just the text directly,
04:55we're gonna need to basically send inside of here
04:57some sort of an object.
04:59And instead of that,
04:59I'm just gonna call this text for the object,
05:02and I'm gonna go ahead and send a parameter,
05:04which we're gonna be the value,
05:06and the value itself will be the actual text,
05:08text thingy that we had there previously.
05:12Now that we're adding it in this type of structure,
05:14we're also gonna very easily,
05:16we could go ahead to our text
05:17and make sure that it starts off with nothing.
05:21So I'm basically clearing it out,
05:22but I wanna make sure I do that
05:23only after I fetch that information,
05:25after I push that item into the pushing logic.
05:29Now that I have a little bit of a different structure,
05:32I can't just call the text directly,
05:33which kind of like implies
05:34why I was doing that previously.
05:36So instead of that,
05:37I'm just gonna go ahead here
05:38and I'll call this maybe object,
05:43and then I'm gonna go ahead here
05:45and just comment this out
05:47and put here another variable,
05:48which I'll call text.
05:49And instead, I'm gonna ask if the object exists.
05:53Excuse me.
05:55If the object exists,
05:56I'm gonna go ahead to my text
05:57and ask for the text itself.
05:59Last but not least,
06:00I'm gonna also go to my text that I want to be
06:03and not approach it from the text directly,
06:05but instead of that,
06:06go to my object.value,
06:07which we've just created, object.value.
06:11Literally sending in the right value
06:14when we need it, storing it,
06:14but also making sure that I'm getting rid of the text
06:17before I do that.
06:18And if I go ahead here and click here on refresh,
06:20we should know if we don't have any errors,
06:22which we don't,
06:23we'll have our copy come in
06:24and then the next one coming in right after it,
06:26exactly the way we wanted it to be.
06:30In this lecture,
06:31we completed building out our animation process
06:33by adding in to the queue auto callbacks
06:36to each animation
06:37and triggering an animation one after each other.
06:40And the next lecture,
06:41we're gonna move on into a new topic,
06:43which will be using animation libraries in general
06:46and not building the animation yourself.

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!