Viewers: in countries Watching now:
In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
Functions are the secondary building block in programming, and allow you to store related tasks in named groups, so they can be reused in the most efficient way possible. In ActionScript, functions are actions, comparable to verbs, so they do things. Now I want you to think of riding a bicycle. You think of riding a bicycle as one task, but it's really a group of related tasks.
So you have sitting on the bike, pedaling, balancing, et cetera. So just like that, a function takes a group of tasks and names in them. They usually have active names, like one of the functions you're familiar with is probably Stop(). Stop is a built-in function. Remember, it's blue, and that stops the timeline. Notice that it's sort of an action word.
Another function is gotoAndPlay(). It looks pretty similar to stop, in that there is a blue phrase and some parentheses and a semicolon, except for in the parentheses you place which frame you want to go to. So gotoAndPlay groups related tasks. It takes the playhead, moves it to a particular frame, in this case 3, and plays from there.
I can go on, but I think you get the point. Now, there is a difference between running a function and creating a function. Running a function is as simple as typing the function name in parentheses. Creating the function is a little bit more complicated. To create a function in Flash, type the word 'function.' Remember, it should be all lowercase, and it should be blue after you type it, showing that it's a keyword reserved for Flash. Type a space, and then type the name of the function.
Let's say I wanted to create a function that added numbers, so I'll call this addNumbers. Usually, a function name starts with some type of action word. Here my action word is add. So it describes what the function does. I also like to write in camel-case, meaning each new word starts with a capital letter. This is useful because you can't use spaces in function names.
After the name of the function, type some parentheses. After the parentheses, you specify what type of data the function will return. We'll look at this in more detail later on, but for now, just type a colon, and then type void. This is going to be one of the very few datatypes that starts with a lowercase letter, so v-o-i-d, with a lowercase v. Go to the next line, type an open curly brace, that's Shift+Square Bracket, and then press Enter or Return.
Depending on your ActionScript settings, Flash may or may not write a curly brace for you, so you might have to add that close curly brace. Inside of the curly braces, I can write my tasks that the function will do. This is usually a set of commands, one on each line. So let's say I wanted to just add a simple trace statement here. So I'll type trace - remember that puts a message in the output window, some parentheses, and a semicolon. In the parentheses, I'm not going to use quotes; I'm just going to type 1 + 2.
So I'm going to trace 1 + 2. Now if I were to test the movie, then nothing would happen at this point, because remember, I said that there is a difference between declaring a function and running a function. So this block of code I have highlighted here is simply declaring a function. It's saying that it exists, and you can run it whenever you want. In order to run the function, go below the curly braces, or of course, you can go above the word 'function.' It doesn't matter; as long as it's in your code, Flash will find where the function is.
Then type the name of the function. So it's addNumbers and some parentheses, and a semicolon. So notice that it looks kind of like the stop function. It has the command and then the parentheses and a semicolon. But it's not blue, because this is a custom function that we just created. So test the movie. There, you can see 3 in the output window. To make a function more reusable, you can add something called parameters.
Remember, when you ran the gotoAndPlay function that you passed in a number that tells Flash what frame to go to and play from. So we'll look at doing that here with our custom function. So let's say when you addNumbers, you're going to put the numbers that you want to add inside of the function. So every time you run the addNumbers function, you can use different numbers, and basically you have the function do something unique each time you run it. Let's look at how that works.
Put your cursor in the parentheses, at the top of your code, right after the open parenthesis after addNumbers; Type num1. So all lowercase, num, n-u-m, and then a 1. This is the first parameter, and I am going to declare its datatype. So I'm going to type a colon, and then I'm going to type the word 'Number' with the capital N. Once I see it's highlighted in the Code Hinting window, I'm just going to press Return, and have Flash complete that for me.
Then I'm going to type a comma, and then I'm going to type a space, and the I'm going to type num2:Number, just like I did for num1 function Most of the time spaces don't matter, so I can either have a space or not have a space here, but I prefer to put spaces because it helps me to read the code a little easier. So we have num1 and num2. Now I'm going to replace these numbers inside of the function with num1 and num2.
So instead of adding 1 + 2, it's going to add num1 + num2. Now these numbers don't have any values yet. They won't have any values until we run the function and tell Flash which values those should be. So go down to the bottom of your code, when you're running the addNumbers function. Right after the open parenthesis, put one number - I'll put 5, put a comma, and notice the Code Hinting window comes up.
It says addNumbers, which is the name of our function. It says there are two values. There is num1 - that is a number, and then there is num2, that is also a number. So for num2, I'll put in 10. So now I'll test the movie, and look in the Output window. So now I have 15. So the magic of the function is that I can reuse this as many times as I want. I can highlight the addNumbers line of code on line 6, copy it with Command+C or Ctrl+C, and then go down and paste it, and repeat that, paste in a few more times, and I can change the numbers that I'm adding to whatever I want.
So now I can test the movie again. For some reason, the Output window scrolled down, and I don't see anything, but there actually is something there. So if I close that window and close the Actions panel and scroll up, you'll see the numbers that were added for me. So you've seen that I can reuse this function as many times as I want. Right now, it's only just one line of code, but imagine if you wrote 20 lines of code inside the function, and instead of having to rewrite 20 lines of code four times, you simply put it into a function, and you can just call the function or run the function four times, passing in different values like we're doing here.
As we go throughout the rest of this title, you'll see more uses of functions and get experience learning how effective they can be. What I want you to remember now is that functions are groups of commands that are named and can be reused to organize and optimize your applications.
Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.