Functions
      by kirupa | 27 December 2005

There are times when you will want to have a section of ActionScript code reused. In those cases, functions play an important role. A function is a section of code that can be reused throughout your movie, and it can be varied depending on the data you input to the function. Think of a function as a DVD player. What the DVD player outputs on the screen depends on your input, the DVD disc. A function behaves in a similar fashion.

A function has the ability to receive data, process that data, display data, send data, and more. Just like your DVD drive, the actual function itself does not change. What changes is the data that goes through it.

Here is an example of an animation that calculates the volume based on the number you input:

[ enter a number in length, width, height, and press the yellow button ]

While you cannot see it immediately, I am using a function to calculate the volume based on the information you input. The following sections will go through several variations of the basic function and how they are implemented.

In the last section, you will receive the code that I used in the above animation and an explanation as to why it worked.


Anatomy of a Basic Function
In Flash, you define a function according to the following format:

name = function () {
    actions
};

The above blue colored words are the portions of a function you will change. In order to access a function or, what's commonly known as calling a function, you simply use the following command:

name();

All of this may seem abstract, so here is an example to bring your mind back to reality! Copy and paste the following code into the Actions window in the first frame of a new Flash movie:

voluminatorizer = function () {
trace("volumizing since 1997");
};
voluminatorizer();

After typing the above, press Ctrl + Enter to see the words "volumizing since 1997" appear in the Output window.

The first section of the code is the actual function. The name of the function is voluminatorizer, and the action of the function is the trace action that displays the words on your screen. The second section is where the function is called - voluminatorizer();

The text is displayed when Flash reaches the line voluminatorizer(). If you were to remove that line, you will find that the text that does not display. That brings about an important point. A function will not run unless it is called.

Another cool feature about functions is that they can be run when you want them to be run. In our previous section, the call line followed the function, but it does not have to be that way. For example, add the following action to your button:

on (release) {
voluminatorizer();
}

Press Ctrl + Enter and click on your button. You will find that the call action to voluminatorizer() runs the function you created in the frame of your movie! That is pretty cool, ehh? As long as your function is declared somewhere in your movie, you can call it by referring to it in the appropriate areas such as in another frame, in a movie clip's event structure, button actions, and any other method that is legally allowed by Flash.

1 | 2

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.