Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we'll write a custom function that will make our snowboarder jump up into the air and turn a little bit. If you'd like to follow along, this in the chapter 3 Exercise Folder and the file is called WritingFunctions.fla. This is a very simple file. It has a movie clip called mcBoarder. You can verify in the library that's the name of the movie clip, and it's just a png image. And I just drew some snow so he can have some turf to ride on. Okay, let's write the code that will make this movie clip move.
So first I'm going to click on this boarder and just make sure that he has an instance name, and there is an instance name and it is boarder_mc. So if we want to communicate with ActionScript to the snowboarder we're going to refer to him as boarder_mc. I'm going to go up to the actions layer, click the first keyframe of that layer. I'm going to make sure it's locked. Select that keyframe, press Option+F9 or F9 on the PC to open on the actions panel. Now what we're going to do is the same thing we did before when we changed properties of an object except we're going to put it inside of a function.
In the next movie we'll explore how to make this function more useful. But for right now, let's just type the word function, and then a space. And I'm going to call this function moveBoarder. Defining a function is similar to defining a variable. Instead of typing var and a space and the variable name, you type function and a space and the function name. So we're going to build our reusable block of code here. So function moveBoarder().
And we need to put parentheses, always when we create function and in the next movie I'll explain what you put inside the parentheses. And then I'm going to type a colon and and that's going to specify the return datatype. I haven't yet explained the return datatype and we'll cover that in a later movie. But I'm going to type void for now, with a lowercase V, that means there is no return datatype. Press Enter or Return to go to the next line. Open curly brace, next line, close curly brace. up arrow, and then Enter or Return.
And inside of these curly braces is where we write what happens in the moveBoarder function. So all I'm going to do is adjust the boarder's position. So I'm going to type boarder_mc, that's our boarder instance name, dot y, and type space equal space, and then type 50; We're going to set the y-positioning of boarder_mc to 50. That's going to be 50 pixels from the top of our Flash movie and not from the bottom.
Remember Flash counts pixels from the top left, not the bottom left like in math class. Now let's rotate our boarders. So I'm going to press Enter or Return to go to the next line. And I'm going to type boarder_mc .rotation space equal space 45, let's type a ; And now if we were to test the movie right now, nothing would happen. Just like I explained at the beginning of this chapter how we had to drag elements inside of the function box and that didn't necessarily run the function. We had to click the run function button. That's what we're doing here. We're putting things inside of the function block of code.
We have not ran the function yet. So we need to do something else to run the function. And that something else is like we did in last movie when we ran go to and stop and we ran the stop function. So I'm going to click right after this curly brace on line 5 and I'm going to press Enter or Return twice to go down 2 lines. You don't have to go down 2 lines, you can go down just one line, I just like to space out my code. And then if we want to run this function, we need to type and moveBoarder, which is the name of our function, (); Notice how that looks exactly like stop.
Stop(); That actually runs the function. So I'm going to close this Actions window. Just take a note of where the boarder is currently. Test the movie and notice that it jumps to the top of the screen. So if you move over the movie, you can notice that he's also rotated. Now that you know how to write a custom function, let's learn how to make our functions more useful.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101314 Viewers
61 Video lessons · 88071 Viewers
71 Video lessons · 71925 Viewers
56 Video lessons · 103750 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.