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

Creating our own functions

In the last few lectures we've been playing around with functions. Although that was a blast, it's time for us to learn how to create our own functions. The most powerful feature of any programming language is functions. With functions grows our capabilities to create amazing things. By the end of this lesson we are going to be deep into the world of animation.

Video Transcript

00:00This is exciting exciting exciting god. Well, so so far we created functions
00:06That was well
00:07We actually worked with functions and you should be familiar and you should be comfortable functions at this stage
00:12And now we're gonna notch it up one level up because at this point in time
00:18We're actually gonna create our own functions
00:21We created a function before without knowing what functions are but now that we're gonna create our function and we're all don't know
00:28What functions are then well, you're gonna have the big picture and it's things are gonna start connecting
00:33So let's start connecting things together
00:35everything
00:37Invisible the next step that I want to do is I want to be able to basically create our own unique function
00:44So we already created a function, but let's create one on our own now that we know what functions really are
00:50They basically deliver an action. They are the action of any programming language
00:55So the way we create a function is by calling the name function in JavaScript and every language
01:01The name is a little bit different. The first step is calling a function and then giving it a name
01:06So for example, let's call this animate in animate in now our animate in function
01:13We always open and close around bracket
01:16That's just what you do with a function and then to be able to say what is inside of this function
01:21We have to put oto a curly bracket open and close one and this is the construction. That's it
01:27Congratulations, you created your first function on your own which is called animate in now
01:32The one thing to note is the opening closed bracket because that's that's how we identify a function
01:37We're gonna see really soon why it's so critical
01:40And the next is we always open around a curly bracket and close a curly bracket
01:45Which tells us this is the boundaries of this function because if we didn't have it here
01:49How would we know where our function starts and where it ends some languages actually have a command called end function?
01:56That doesn't exist in JavaScript. I think that is in
02:01Visual basic, but literally by putting an open bracket a open curly bracket and a closing curly bracket
02:08We're bounding the function basically saying hey
02:11This function which is named animate in starts right here and it finishes right here
02:16And that's really the the point behind the closing opening and closing brackets
02:21now what I want to do next is I want to use one of the built-in functions of tween max well not built-in but built-in into
02:28The screen tracks max library, but what I want to do is I want to call that to function
02:32We've which we've met earlier and I want to let's say animate everything here
02:37So I basically want to take all those items all those three items
02:40I want to animate them in together or how about I'll just animate two out of the three that works for me as well
02:45And I'm going to set the how long do I want it to take one second and I'll set the opacity to be one
02:52So I'm just going to copy that code there and again
02:54I'm just going to I'm skimming through the things we didn't really talk about yet that we didn't technically learn yet
03:00But in our function here that we're calling we're calling a function that accepts three parameters
03:04Our first parameter is what in this case?
03:07We're going to animate two things
03:08The second is how long is it going to take and the last is what are we going to animate and in our case?
03:13We want to animate the opacity back to one and that is our animate in function now
03:19We do have a slight little problem
03:22Because if I just call my animate if I just run my application and I save it. We'll see that nothing will happen
03:28So if I go ahead go into my browser click on refresh
03:31We'll see that still nothing is going on now
03:34The reason is is because we never actually called triggered our function. We just
03:39We just created it so this is a function that was created that does not mean that we actually called it
03:52Now contrary to variables where when you set a variable and I want to show you something when you create a variable
03:59It's I called it a and I said the value 10 inside of it
04:02But if I go to just before that and I go into my console
04:06Dot log and I want to log out the value of a
04:09Back into the console and I go back and I refresh my app look what will happen
04:14I'm gonna get the value undefined, which means there's nothing inside there or we don't know what this variable is
04:20So literally for a variable you can only call it after it's been defined
04:41contrary to that a
04:42Function could be called at any point because as soon as the application is loaded
04:48The function is created so I can go ahead right underneath here
04:51even though it's above our animate in and I could literally just call animate in and
04:57Open and close bracket to trigger it and by doing that I'm going to trigger that function which would then create my animation
05:04So if I go ahead I save this and I go ahead and refresh
05:08We're going to see that now our animation is really going to animate in
05:12So let's continue this and let's create another line into our animate in and we're going to create a secondary line where we're going
05:19to animate my image as well and
05:22This time around for my image. I'm going to go ahead and set my image animation time to be two seconds or
05:29You know even better how about we learn about a new? Oh, let's do this and then we'll learn about a new property
05:35All right. There we go a new a new method
05:37So we see that now all the three are going together, which is lovely, but not necessarily what we want it to do
05:58So let's go back into our application
05:59What I really want happening is I want this to take one second, but I don't want it to happen in this stage
06:04I want it to animate in a little bit later. So I'll create another function. Let's do that together
06:09I'm going to create another function and I'm going to call it animate in
06:13image and
06:16I'm going to put inside my end my animation of the image into it's now have another function and by the way
06:21You could have two lines and you could have as many lines of code as you want or you could have only one
06:25That's completely fine. Now what I want to do is I want to call my animate in image
06:30But I don't want to call it immediately. I want to give it a slight delay
06:34to do that, I'm going to call the tween max
06:38tween max library and I'm going to use the function delay and for the delay function
06:44I'm going to say what how much of a delay delay of one second. What function do I want to call?
06:49I want to call the animate in
06:51Image now by doing that if I save this and I go back into my application and click on refresh
06:56What's going to happen is this is going to oh and you see that I have your a slight
07:01To call is actually called delayed call not delay. We're not going to talk about delay right now delayed call
07:28Let me just make sure
07:31All right delayed call and let me just save that I'm going to go back in there and just refresh again
07:36That's really the important of checking your documentation and notice what happened our elements first loaded in then our delayed call called our second
07:44function which was a little bit afterwards
07:47So what we've seen here is one is this not only do
07:51Could we separate things by action, but because we're separating it into separate functions
07:55We add the capability of delaying a call for example saying hey
07:59We want to do this a little bit later now, obviously, this is a very simplified example
08:04But the more functions you have and as we kind of progress and create more sophisticated functions
08:11It's going to become more and more important. For example, how about once we finish this animation?
08:16What if we want to create another animation, but this time we want to animate a different property
08:22So I could literally if I want to figure out what property I could go ahead here and just select the
08:27Item here and just right-click on it and select inspect and firebug and then I can come here into my elements into my image
08:34And just add here an example of property that I want for example if my bottom
08:38Let's see what happens if I lowered the value to minus
08:41A hundred and eighty minus two hundred
08:44All right, so let's see it then this is what we're gonna do in the next the next section
08:48We're gonna continue working with this function
08:50And what I want to do is I want to create further functions that one will let's see animate the item back to the bottom
08:55and how about another animation another a function that would switch the
09:00Element on the stage and then plop it back up and put a different image above so in the next lecture
09:05We're gonna continue working with what we learned so far adding a few little small things
09:10To create a few more layered a few more steps into our animation
09:14So we'll see you in the next lecture where we continue that steps

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!