Join Todd Perkins for an in-depth discussion in this video Using and creating functions, part of Flash CS4 Professional: Object-Oriented Programming .
Let's take a look at ActionScript 3.0 functions. I'm going to go to the first keyframe in the Actions layers in my file here and open up the Actions panel by pressing Option+F9 on the Mac or F9 on the PC. You define a function in ActionScript 3.0 by using the keyword function. So it's all lowercase. Just type function and then a space and the name of your function. So let's say I call it myFunction. Then you type some parenthesis, a colon and define the return data type.
If the function has no return data, the return data type is void and that's all lowercase. The function is then defined within curly braces. So type open and closed curly braces and here we put the function code. So let's say my function code simply traces some numbers. So it's going to trace (3+4); and in ActionScript 3.0 you run a function by calling the function's name. So myFunction(); So I'll test the movie.
Command+Return on the Mac, Ctrl+Enter on the PC. And I'll see the result in the Output window, which is 7. That's the result of 3 + 4. Functions in ActionScript 3.0 can hold parameters, which are also knows as arguments. So let's say I'm going to pass in two numbers. Num1, I'm going data type it as a Number. So I'm going to put that in the parenthesis where I define the function and then I'm going to define another parameter by typing in a comma and then the name of second parameter. So num2:Number. Now when I run the function, I'll need to pass in a value for num1 and num2.
So it will be a number, a comma and then another number. So let's say I wanted to trace three plus four so I'll type 3, 4 and then I'm going to replace the 3 and the 4 with num1 and num2 inside the function. Then I'll test the movie using Command+Return on the Mac or Ctrl+Enter on the PC. I'll see the same result in the Output window. Functions in ActionScript can also return values. So let's say I wanted to return a number. I'm going to change the return data type from void to Number and I'm going to change that trace statement to return num1+num2 using the return keyword. So I'll type return num1 + num2 and I'll make sure to delete that closed parenthesis there.
Now what I can do is wrap the call to myFunction in a trace statement here. So I'll type trace and then some parenthesis around the call to myFunction. Then we will see that same result in the Output window. So basically what's happening is myFunction runs, the data is returned, and then myFunction or the value of the call to myFunction here is 7, when we pass in 3 and 4. You could also use a function call that's a value for a variable. So let's say I create a variable above this function called myNum. Data type it to Number and I'll set it equal to myFunction(5,6);. So the data type for this function when I run it is a Number, because its return data type is Number.
So now if I trace the value of myNum, I'm going to get the result that is returned from running myFunction's passing in 5 and 6, which when I test the movie should be 11 in the Output window. So to create a function use the function keyword, type a space, the name of the function, parenthesis with any arguments or parameters inside of the parenthesis separated by commas. After the parenthesis declare the return data type of the function using a colon. If there is no return data, the data type is void. If you do have a return data type, you will need a return statement inside of your function.
A return statement just has the word return and space, then the value to return. The value needs to be the same data type as the return data type of the function. Then to run a function you just type in name of the function, parenthesis, then any parameters you are passing in separated by commas.
- Loading external text, images, and XML files
- Creating essential properties for a View class
- Defining a Model class
- Building and implementing interfaces and designs
- Debugging projects
- Building applications with Controller-class buttons
Skill Level Intermediate
1. ActionScript 3.0 Essentials
2. Advanced ActionScript Techniques
3. Object-Oriented Programming Fundamentals
4. The Model View Controller Design Pattern
5. Building a Model Class
6. Building a View Class
7. Building a Controller Class
8. Improving and Debugging the Application
9. Moving Forward
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.