ActionScript Fundamentals pt. 1 / About Methods & Functions
Subtitles of the Movie
Let me now review two more ActionScript concepts: Methods and Functions. Continuing our review of Classes and we had properties of those classes, such as a person who as red hair or green eyes. A person can also do various things such as walk, read, clean, or dance. These would be examples of a persons' methods or actions. For example, a person cannot fly, that method would belong to a different kind of object, such as a bird. Methods then are predefined Functions that are part of a class definition. Methods are bound to the Classes that they help define. For instance the MovieClip Class has a gotoAndStop Method. We cannot use this method for the date Class. Now the Stop method is one of the most common methods. It's a built-in function of the MovieClip Class. It tells a particular Timeline to stop where it is. Also have a related method, the gotoAndStop and then there's a parameter, something called the parameter, inside those parentheses. More about parameters in a moment. In this case the method is telling the Playback Head to go to and stop at a label, a frame label, called page1. Now we used functions to good effect in our first script as well, so let's talk about Functions now. This is the function that we used: function handleClick and then a MouseEvent void. We used this function to determine which button had been clicked and then to move the Playhead to the appropriate frame using these conditionals here. If the first button was pressed, actually if the page1 button was pressed it's going to go to Page 1; if the page2 button was pressed go to the label page2 and so on. So, functions are reusable containers for specific actions. You'll often need to write code that will be used numerous times and functions allow you to group that code in convenient containers rather than having to manually type it out each time you need it. For example, let's say that a person has a daily routine that includes waking, dressing, eating breakfast, going to her job and then eating lunch. In ActionScript these actions, or methods, might look like the following code. Notice that each of these has a method, wake, dress, breakfast, job and lunch, as well as an object, that's the person right there, of the person Class; each has an open and closed parentheses and then a semicolon at the end; more about this syntax in a little while. Now this person may go through this routine every day of the typical work week, but rather than retyping these five lines of code five times, once for each day of the week, we can create a Function that holds these actions and then reference the Function itself. For example, here's my function called weekday. It includes all of these actions or methods. Now we can reference the weekday function as follows: the word function, the name of the function, myMonday, open-close parentheses and then run the weekday scripts right here, that's the weekday scripts. Here's the function for Tuesday, exactly the same, weekday, open-close parentheses semicolon, notice the curly braces as well. You can see that this is a much more efficient way of doing it than calling out these five methods for each day of the week. Now notice the syntax for constructing a function. It's as follows: we start with the function keyword right here and then followed by the custom name of the function, in this case they're using Camel Case, but we could also do underscore lower case n there. We want to be sure to follow the same naming rules previously given. No spaces or strange characters and keep everything probably lower case. We follow the function name with the open and closing parentheses. Within those parentheses you can provide parameters, which we'll examine shortly. After the parentheses we type an open curly brace, then we type in all the actions here, would go right here and then we close out the function with a closing curly brace and then don't forget the closing semicolon right there at the end. Let's talk a little bit more about these optional parameters right here and let me define the term parameters. Now when you construct a function you have the option of defining what's called Parameters for that function. Parameters are preset forms of data that impact how the function runs. Now the function parameters that we used in our first script came with the gotoAndStop method, also its close relative gotoAndPlay. These two functions have parameters that represent several kinds of data. In our case we used a string to refer to a frame label, but I could have also used a whole number to represent a frame number. I could have also included a scene name in parentheses as well as a frame name, or I could have mixed the parameters. I could have used a scene string in a frame number like so, the 5 then would not be in quotations here, like so. Notice it doesn't go green. Now in these examples, the frame numbers, the frame labels and the scene names are all parameters that impact where the Playhead goes to before stopping. Note that not every parameter is required. In the case of gotoAndStop at least one parameter is required for the function to execute correctly, however, notice that the stop method right here doesn't require anything in the parameters there, in the parameter field, that's where the open and closed parentheses are. Now when you define a function's parameters they should always be strictly typed to tell the script what kind of data will be involved, whether it's a string or a number, a whole number, a fraction, or what have you. More about these data types shortly, but after the parameter name add a colon and then the data type. This would look like the following. Let me go down here to the bottom and type in the script: myParameter colon and then in this case it's a String data type. Now Parameters are a type of variable since their values can change, so let's explore this important concept of variables in the next movie.
Tutorial Information
| Course: | Adobe Flash ActionScript 3.0 for Designers |
| Author: | James Gonzalez |
| SKU: | 34060 |
| ISBN: | 1-935320-82-3 |
| Release Date: | 2009-11-09 |
| Duration: | 9.5 hrs / 101 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 